简体   繁体   中英

How to replace the nth column/field in a comma-separated string using sed/awk?

assume I have a string

"1,2,3,4"

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

"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:

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!

Have the shell interpolate the index in the awk program:

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.

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

Here's a sed uctive way to break the awk wardness:

$ 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/'

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

With plain awk (IE 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM