我有一个包含 3 列的列表:
2547123456789,5391074043372870,639027123456789
我想像这样修改第二列:
sed 's/\([0-9]\)\([0-9]\)/\2\1/g' so that it becomes:
3519700434738207
我的问题是如何在 awk/sed 的一行中执行此操作,同时保持其他列不变,以便我的最终文件具有:
2547123456789,3519700434738207,639027123456789
谢谢
最佳答案
试试这个:
sed 'h;s/.*,\([^,]*\),.*/\1/;s/\(.\)\(.\)/\2\1/g;G;s/\([^\n]*\)\n\([^,]*,\)[^,]*\(.*\)/\2\1\3/' inputfile
说明:
# copy the line to hold space
h;
# capture the second column and retain it in pattern space
s/.*,\([^,]*\),.*/\1/;
# swap each pair of characters
s/\(.\)\(.\)/\2\1/g;
# append the contents of hold space, now pattern space looks like
# new_field_2[newline]field_1,old_field_2,field_3
G;
# capture the fields that we want and arrange them in the proper order
s/\([^\n]*\)\n\([^,]*,\)[^,]*\(.*\)/\2\1\3/
关于awk - 仅使用 awk 和 sed 修改第 2 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4525001/