linux 逗号分隔,linux-如何用逗号分割列表而不是sp

本文讨论了如何在Linux环境下,利用bash脚本以逗号而不是空格来分割CSV文件中的列表。提供了多种解决方案,包括修改IFS变量、使用awk、sed和while循环等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux-如何用逗号分割列表而不是sp

我想用,分隔文本,而不是for foo in list中的。假设我有一个CSV文件CSV_File,其中包含以下文本:

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

...

我使用以下代码将其拆分为几个词:

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

do echo $word

done

它打印:

Hello

World

Questions

Answers

bash

shell

script

但我希望它用逗号而不是空格分隔文本:

Hello

World

Questions

Answers

bash shell

script

我如何在bash中实现这一目标?

7个解决方案

48 votes

将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:~$

Sorin answered 2020-07-29T02:47:50Z

46 votes

使用subshell替换来解析单词会撤消将空格放在一起的所有工作。

请尝试:

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

echo $word

done

这也增加了并行度。 在问题中使用subshell会强制完成整个subshell过程,然后才能开始遍历答案。 通过管道连接到子外壳(如我的回答),它们可以并行工作。 当然,这仅在文件中有很多行时才重要。

mkj answered 2020-07-29T02:47:30Z

17 votes

我认为规范的方法是:

while IFS=, read field1 field2 field3 field4 field5 field6; do

do stuff

done < CSV.file

如果您不知道或不在乎有多少个字段:

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

glenn jackman answered 2020-07-29T02:48:14Z

10 votes

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

Kent answered 2020-07-29T02:48:30Z

7 votes

创建一个bash函数

split_on_commas() {

local IFS=,

local WORD_LIST=($1)

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

...这将产生以下输出:

Item: this

Item: is a

Item: list

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

Andrew Newdigate answered 2020-07-29T02:48:58Z

5 votes

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

IFS内部字段分隔符,用于单词拆分 扩展后,将行与单词拆分成单词 内置命令。 默认值为``''。

IFS是一个Shell环境变量,因此它将在Shell脚本的上下文中保持不变,但在其他情况下将保持不变,除非您将其导出。 还请注意,IFS根本不会从您的环境继承:请参阅此gnu帖子,以获取有关IFS的原因和更多信息。

您的代码是这样写的:

IFS=","

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

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

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

Ashley Raiteri answered 2020-07-29T02:49:37Z

0 votes

您可以使用:

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

要么

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

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

sed 's/,/ /g'

ozma answered 2020-07-29T02:50:05Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值