如何使用sed / awk替换逗号分隔的字符串中的第n列/字段?

时间:2022-10-16 00:20:51

assume I have a string

假设我有一个字符串

"1,2,3,4"

Now I want to replace, e.g. the 3rd field of the string by some different value.

现在我想替换,例如字符串的第3个字段由一些不同的值组成。

"1,2,NEW,4"

I managed to do this with the following command:

我设法使用以下命令执行此操作:

echo "1,2,3,4" | awk -F, -v OFS=, '{$3="NEW"; print }'

Now the index for the column to be replaced should be passed as a variable. So in this case

现在要替换的列的索引应该作为变量传递。所以在这种情况下

index=3

How can I pass this to awk? Because this won't work:

我怎么能把这个传递给awk?因为这不起作用:

echo "1,2,3,4" | awk -F, -v OFS=, '{$index="NEW"; print }'
echo "1,2,3,4" | awk -F, -v OFS=, '{$($index)="NEW"; print }'
echo "1,2,3,4" | awk -F, -v OFS=, '{\$$index="NEW"; print }'

Thanks for your help!

谢谢你的帮助!

5 个解决方案

#1


6  

Have the shell interpolate the index in the awk program:

让shell在awk程序中插入索引:

echo "1,2,3,4" | awk -F, -v OFS=, '{$'$index'="NEW"; print }'

Note how the originally single quoted awk program is split in three parts, a single quoted beginning '{$', the interpolated index value, followed by the single quoted remainder of the program.

请注意原始单引号awk程序如何分为三部分,单引号开头'{$',插值索引值,后跟程序的单引号余数。

#2


5  

This might work for you:

这可能对你有用:

index=3 
echo "1,2,3,4" | awk -F, -v OFS=, -v INDEX=$index '{$INDEX="NEW"; print }'

or:

要么:

index=3 
echo "1,2,3,4" | sed 's/[^,]*/NEW/'$index

#3


1  

Here's a seductive way to break the awkwardness:

这是一种打破尴尬的诱人方式:

$ echo "1,2,3,4" | sed 's/,/\n/g' | sed -e $index's/.*/NEW/'

This is easily extendable to multiple indexes just by adding another -e $newindex's/.*/NEWNEW/'

只需添加另一个-e $ newindex的/.*/ NEWNEW /',就可以轻松扩展到多个索引

#4


0  

# This should be faster than awk or sed.
str="1,2,3,4"
IFS=','
read -a f <<< "$str"
f[2]='NEW'
printf "${f[*]}"

#5


-1  

With plain awk (I.E. Not gawk etc) I believe you'll have to use split( string, array, [fieldsep] ); change the array entry of choice and then join them back together with sprintf or similar in a loop.

使用普通的awk(I.E. Not gawk等)我相信你将不得不使用split(string,array,[fieldsep]);更改选择的数组条目,然后将它们与sprintf或类似的循环连接在一起。

gawk allows you to have a variable as a field name, $index in your example. See here.

gawk允许您在示例中将变量作为字段名称$ index。看这里。

gawk is usually the default awk on Linux, so change your invocation to gawk "script" and see if it works.

gawk通常是Linux上的默认awk,所以将你的调用更改为gawk“script”并查看它是否有效。

#1


6  

Have the shell interpolate the index in the awk program:

让shell在awk程序中插入索引:

echo "1,2,3,4" | awk -F, -v OFS=, '{$'$index'="NEW"; print }'

Note how the originally single quoted awk program is split in three parts, a single quoted beginning '{$', the interpolated index value, followed by the single quoted remainder of the program.

请注意原始单引号awk程序如何分为三部分,单引号开头'{$',插值索引值,后跟程序的单引号余数。

#2


5  

This might work for you:

这可能对你有用:

index=3 
echo "1,2,3,4" | awk -F, -v OFS=, -v INDEX=$index '{$INDEX="NEW"; print }'

or:

要么:

index=3 
echo "1,2,3,4" | sed 's/[^,]*/NEW/'$index

#3


1  

Here's a seductive way to break the awkwardness:

这是一种打破尴尬的诱人方式:

$ echo "1,2,3,4" | sed 's/,/\n/g' | sed -e $index's/.*/NEW/'

This is easily extendable to multiple indexes just by adding another -e $newindex's/.*/NEWNEW/'

只需添加另一个-e $ newindex的/.*/ NEWNEW /',就可以轻松扩展到多个索引

#4


0  

# This should be faster than awk or sed.
str="1,2,3,4"
IFS=','
read -a f <<< "$str"
f[2]='NEW'
printf "${f[*]}"

#5


-1  

With plain awk (I.E. Not gawk etc) I believe you'll have to use split( string, array, [fieldsep] ); change the array entry of choice and then join them back together with sprintf or similar in a loop.

使用普通的awk(I.E. Not gawk等)我相信你将不得不使用split(string,array,[fieldsep]);更改选择的数组条目,然后将它们与sprintf或类似的循环连接在一起。

gawk allows you to have a variable as a field name, $index in your example. See here.

gawk允许您在示例中将变量作为字段名称$ index。看这里。

gawk is usually the default awk on Linux, so change your invocation to gawk "script" and see if it works.

gawk通常是Linux上的默认awk,所以将你的调用更改为gawk“script”并查看它是否有效。