How to split a list by comma not space

2019-03-09 11:02发布

I want to split a text with comma , not space in for foo in list. Suppose I have a CSV file CSV_File with following text inside it:

Hello,World,Questions,Answers,bash shell,script
...

I used following code to split it into several words:

for word in $(cat CSV_File | sed -n 1'p' | tr ',' '\n')
do echo $word
done

It prints:

Hello
World
Questions
Answers
bash
shell
script

But I want it to split the text by commas not spaces:

Hello
World
Questions
Answers
bash shell
script

How can I achieve this in bash?

7条回答
Anthone
2楼-- · 2019-03-09 12:01
kent$  echo "Hello,World,Questions,Answers,bash shell,script"|awk -F, '{for (i=1;i<=NF;i++)print $i}'
Hello
World
Questions
Answers
bash shell
script
查看更多
登录 后发表回答