仅使用awk和sed修改第2列

仅使用awk和sed修改第2列,awk,sed,Awk,Sed,我有一个包含3列的列表: 2547123456789,5391074043372870,639027123456789 我想修改第二列,如下所示: sed 's/\([0-9]\)\([0-9]\)/\2\1/g' so that it becomes: 3519700434738207 我的问题是如何在awk/sed的一行中执行此操作,同时保持其他列不变,以便最终文件具有: 2547123456789,3519700434738207,639027123456789 谢谢 输出 awk

我有一个包含3列的列表:

2547123456789,5391074043372870,639027123456789
我想修改第二列,如下所示:

sed 's/\([0-9]\)\([0-9]\)/\2\1/g' so that it becomes:

3519700434738207
我的问题是如何在awk/sed的一行中执行此操作,同时保持其他列不变,以便最终文件具有:

2547123456789,3519700434738207,639027123456789
谢谢

输出
awk-F,'{printf(“%s,”,$1);len=split($2,a,”);for(i=1;i试试这个:

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/

您接受的答案与您要求的结果不一致。它只翻转了第二个字段的前2个字符。如果这是您真正想要的,请更新您的问题以反映这一点。谢谢我误读了问题,认为
g
标志将拾取第二列。如果OP能够确认他想要的内容,我将elete我的回答…+1,有一刻我觉得用sed一行是不可能的;)工作得很有魅力:-)谢谢
awk -F, '{printf("%s,",$1);len=split($2,a,"");for(i=1;i<len;i+=2)printf("%s%s",a[i+1],a[i]);printf(",%s\n",$3)}' ./infile
2547123456789,3519700434738207,639027123456789
1234567890123,2143658709214365,123456789012345
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/