views:

224

answers:

3

I'm currently using the following to split a file into words - Is there some quicker way?

while read -r line
do
    for word in $line
    do
        words="${words}\n${word}"
    done
done
+4  A: 

What about using tr?

tr -s [:space:] \n < myfile.txt

The -s squeezes multiple whitespace characters into one. You might need to escape some things to execute it.

scompt.com
+1 for this solution. One minor detail: Use \\n, "\n" or '\n' instead of the unquoted \n
Hai Vu
+1, definitely the fastest solution
l0b0
A: 
xargs -n 1  echo <myfile.txt
Jürgen Hötzel
Thanks, but this solution is slower than the others.
l0b0
+1  A: 
sed 's/[[:space:]]/\n/g' file.txt
Dennis Williamson