Linux 如何用逗号而不是空格分割列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7718307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:35:59  来源:igfitidea点击:

How to split a list by comma not space

linuxbashshellcsvsplit

提问by Eng.Fouad

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

我想分割与逗号文本,没有空间for foo in list。假设我有一个 CSV 文件,CSV_File其中包含以下文本:

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?

我怎样才能在 bash 中实现这一点?

采纳答案by mkj

Using a subshell substitution to parse the words undoes all the work you are doing to put spaces together.

使用 subshel​​l 替换来解析单词会撤消您为将空格放在一起所做的所有工作。

Try instead:

试试吧:

cat CSV_file | sed -n 1'p' | tr ',' '\n' | while read word; do
    echo $word
done

That also increases parallelism. Using a subshell as in your question forces the entire subshell process to finish before you can start iterating over the answers. Piping to a subshell (as in my answer) lets them work in parallel. This matters only if you have many lines in the file, of course.

这也增加了并行性。在您的问题中使用 subshel​​l 会强制整个 subshel​​l 过程在您开始迭代答案之前完成。管道到子外壳(如我的回答)让它们并行工作。当然,这仅在文件中有很多行时才重要。

回答by Kent

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

回答by Sorin

Set IFSto ,:

IFS设置为,:

sorin@sorin:~$ IFS=',' ;for i in `echo "Hello,World,Questions,Answers,bash shell,script"`; do echo $i; done
Hello
World
Questions
Answers
bash shell
script
sorin@sorin:~$ 

回答by Ashley Raiteri

Read: http://linuxmanpages.com/man1/sh.1.php& http://www.gnu.org/s/hello/manual/autoconf/Special-Shell-Variables.html

阅读:http: //linuxmanpages.com/man1/sh.1.php& http://www.gnu.org/s/hello/manual/autoconf/Special-Shell-Variables.html

IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is ``''.

IFS 内部字段分隔符,用于在扩展后进行分词,并使用 read 内置命令将行拆分为单词。默认值为“”。

IFS is a shell environment variable so it will remain unchanged within the context of your Shell script but not otherwise, unless you EXPORT it. ALSO BE AWARE, that IFS will not likely be inherited from your Environment at all: see this gnu post for the reasons and more info on IFS.

IFS 是一个 shell 环境变量,所以它在你的 shell 脚本的上下文中将保持不变,除非你导出它。另请注意,IFS 根本不可能从您的环境中继承:有关原因和有关 IFS 的更多信息,请参阅此 gnu 帖子。

You're code written like this:

你的代码是这样写的:

IFS=","
for word in $(cat tmptest | sed -n 1'p' | tr ',' '\n'); do echo $word; done;

should work, I tested it on command line.

应该可以工作,我在命令行上对其进行了测试。

sh-3.2#IFS=","
sh-3.2#for word in $(cat tmptest | sed -n 1'p' | tr ',' '\n'); do echo $word; done;
World
Questions
Answers
bash shell
script

回答by glenn Hymanman

I think the canonical method is:

我认为规范的方法是:

while IFS=, read field1 field2 field3 field4 field5 field6; do 
  do stuff
done < CSV.file

If you don't know or don't care about how many fields there are:

如果您不知道或不关心有多少个字段:

IFS=,
while read line; do
  # split into an array
  field=( $line )
  for word in "${field[@]}"; do echo "$word"; done

  # or use the positional parameters
  set -- $line
  for word in "$@"; do echo "$word"; done

done < CSV.file

回答by Andrew Newdigate

Create a bash function

创建一个 bash 函数

split_on_commas() {
  local IFS=,
  local WORD_LIST=()
  for word in "${WORD_LIST[@]}"; do
    echo "$word"
  done
}

split_on_commas "this,is a,list" | while read item; do
  # Custom logic goes here
  echo Item: ${item}
done

... this generates the following output:

...这会生成以下输出:

Item: this
Item: is a
Item: list

(Note, this answer has been updated according to some feedback)

(请注意,此答案已根据一些反馈进行了更新)

回答by ozma

You can use:

您可以使用:

cat f.csv | sed 's/,/ /g' |  awk '{print  " / " }'

or

或者

echo "Hello,World,Questions,Answers,bash shell,script" | sed 's/,/ /g' |  awk '{print  " / " }'

This is the part that replace comma with space

这是用空格替换逗号的部分

sed 's/,/ /g'