Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string by newline and space in Bourne shell

Tags:

shell

sh

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
like image 377
l0b0 Avatar asked Jan 23 '26 04:01

l0b0


2 Answers

What about using tr?

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

The -s squeezes multiple whitespace characters into one.

like image 159
Edward Dale Avatar answered Jan 24 '26 19:01

Edward Dale


xargs -n 1  echo <myfile.txt
like image 34
Jürgen Hötzel Avatar answered Jan 24 '26 18:01

Jürgen Hötzel