使用sed从页眉和页脚中删除尾随逗号

时间:2014-09-23 09:17:38

标签: csv unix sed

我想从文件的页眉和页脚中删除逗号。我正在使用以下命令删除逗号,但它会扫描文件中的所有行,这是我不想要的。

cat DBS_xx_2014-09-23_234.csv | sed 's/[,\t]*$//' > output.csv

输入文件:

000N5          DBS   103151201409220007770001,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
10,1256,19700802,,,,SMITH,,ABC,,1,,,,,,GX1 4NL  
10,1256,19690802,,,,WilliAM,,XX,,1,,,,,,Gl15 4MX  
101RN5      DBS      103151201409220007770001,,,,,,,,,,,,,,,,,,,,,,,,,,,, 

预期产出:

000N5          DBS   103151201409220007770001
10,1256,19700802,,,,SMITH,,ABC,,1,,,,,,GX1 4NL  
10,1256,19690802,,,,WilliAM,,XX,,1,,,,,,Gl15 4MX  
101RN5      DBS      103151201409220007770001

1 个答案:

答案 0 :(得分:0)

以下sed命令将仅从第一行和最后一行删除所有逗号。

$ sed '1s/,\+//g;$s/,\+//g' file
000N5          DBS   103151201409220007770001 
10,1256,19700802,,,,SMITH,,ABC,,1,,,,,,GX1 4NL  
10,1256,19690802,,,,WilliAM,,XX,,1,,,,,,Gl15 4MX  
101RN5      DBS      103151201409220007770001