Split string into an array in Bash

后端 未结 22 2771
故里飘歌
故里飘歌 2020-11-22 04:24

In a Bash script I would like to split a line into pieces and store them in an array.

The line:

Paris, France, Europe

I would like

22条回答
  •  我寻月下人不归
    2020-11-22 04:59

    I came across this post when looking to parse an input like: word1,word2,...

    none of the above helped me. solved it by using awk. If it helps someone:

    STRING="value1,value2,value3"
    array=`echo $STRING | awk -F ',' '{ s = $1; for (i = 2; i <= NF; i++) s = s "\n"$i; print s; }'`
    for word in ${array}
    do
            echo "This is the word $word"
    done
    

提交回复
热议问题