如何使用 sed 删除 csv 文件每一行中的第二个逗号?

如何使用 sed 删除 csv 文件每一行中的第二个逗号?

我的目录中有一堆 csv 文件,我想从每一行中删除第二个逗号。例子:

输入:

53,900,1001,-45.4247,9.87187,541.65
75,900,0102,37.0896,4.24087,558.35
75,900,1101,37.3096,4.25814,561.65
46,901,0003,-51.3833,-20.2645,578.35
3,901,0003,-57.302,-23.0063,578.35

期望的输出:

53,9001001,-45.4247,9.87187,541.65
75,9000102,37.0896,4.24087,558.35
75,9001101,37.3096,4.25814,561.65
46,9010003,-51.3833,-20.2645,578.35
3,9010003,-57.302,-23.0063,578.35

这可以用 sed 实现吗?提前谢谢了!

答案1

这边走 :

sed 's/,//2' file.csv

该选项已记录这里作为命令可用的标志s

数字

仅替换正则表达式的第数字匹配项。

答案2

awk -F "," 'BEGIN{OFS=","}{$2=$2$3;$3="";gsub(/,,/,",",$0);print}' file.txt

输出

53,9001001,-45.4247,9.87187,541.65
75,9000102,37.0896,4.24087,558.35
75,9001101,37.3096,4.25814,561.65
46,9010003,-51.3833,-20.2645,578.35
3,9010003,-57.302,-23.0063,578.35

答案3

GNU awk cam 的 gensub 函数也可以在这种情况下使用:

$ gawk '{$0=gensub(/,/,"",2)}1' file

相关内容