How to split a line into words separated by one or more spaces in bash?

Bash

Bash Problem Overview


I realize how to do it in python, just with

line = db_file.readline()
ll=string.split(line)

but how can I do the same in bash? is it really possible to do it in a so simple way?

Bash Solutions


Solution 1 - Bash

s='foo bar baz'
a=( $s )
echo ${a[0]}
echo ${a[1]}
...

Solution 2 - Bash

If you want a specific word from the line, awk might be useful, e.g.

$ echo $LINE | awk '{print $2}'

Prints the second whitespace separated word in $LINE. You can also split on other characters, e.g.

$ echo "5:6:7" | awk -F: '{print $2}'
6

Solution 3 - Bash

It depends upon what you mean by split. If you want to iterate over words in a line, which is in a variable, you can just iterate. For example, let's say the variable line is this is a line. Then you can do this:

for word in $line; do echo $word; done

This will print:

this
is
a
line

for .. in $var splits $var using the values in $IFS, the default value of which means "split blanks and newlines".

If you want to read lines from user or a file, you can do something like:

cat $filename | while read line
do
    echo "Processing new line" >/dev/tty
    for word in $line
    do
        echo $word
    done
done

For anything else, you need to be more explicit and define your question in more detail.

Note: Edited to remove bashism, but I still kept cat $filename | ... because I like it more than redirection.

Solution 4 - Bash

echo $line | tr " " "\n"

gives the output similar to those of most of the answers above; without using loops.


In your case, you also mention ll=<...output...>,
so, (given that I don't know much python and assuming you need to assign output to a variable),

ll=`echo $line | tr " " "\n"`

should suffice (remember to echo "$ll" instead of echo $ll)

Solution 5 - Bash

do this

while read -r line
do
  set -- $line
  echo "$1 $2"
done <"file"

$1, $2 etc will be your 1st and 2nd splitted "fields". use $@ to get all values..use $# to get length of the "fields".

Solution 6 - Bash

$ line="these are words"
$ ll=($line)
$ declare -p ll  # dump the array
declare -a ll='([0]="these" [1]="are" [2]="words")'
$ for w in ${ll[@]}; do echo $w; done
these
are
words

Solution 7 - Bash

More simple,

echo $line | sed 's/\s/\n/g'

> \s --> whitespace character (space, tab, NL, FF, VT, CR). In many > systems also valid [:space:] > > \n --> new line

Solution 8 - Bash

The -a option of read will allow you to split a line read in by the characters contained in $IFS.

Solution 9 - Bash

If you already have your line of text in a variable $LINE, then you should be able to say

for L in $LINE; do
   echo $L;
done

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionasdfView Question on Stackoverflow
Solution 1 - BashZoogieZorkView Answer on Stackoverflow
Solution 2 - BashxioxoxView Answer on Stackoverflow
Solution 3 - BashAlok SinghalView Answer on Stackoverflow
Solution 4 - BashgawkfaceView Answer on Stackoverflow
Solution 5 - Bashghostdog74View Answer on Stackoverflow
Solution 6 - BashDennis WilliamsonView Answer on Stackoverflow
Solution 7 - BashcturielView Answer on Stackoverflow
Solution 8 - BashIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 9 - BashPhil MillerView Answer on Stackoverflow