sed back-reference不起作用

时间:2017-08-11 05:17:59

标签: sed

我需要处理曲线CSV的每一行,以删除仅以10个逗号开头的那些行的最后一列。我使用的sed命令是:

$ cat curves.csv
(...)
,,,,,,,,,,2017/10/18,20630.000000
,,,,,,,,,,2017/11/15,20595.000000
,usdSN,:usdSN,,,,8005,$,,2017/08/07,Settlement Date
,,,,,,,,,,2017/12/20,20575.000000
,,,,,,,,,,2018/01/17,20555.000000
,,,,,,,,,,2018/02/21,20535.000000
(...)
,,,,,,,,,,2018/12/21,20290.000000
,usdZS,:usdZS,,,,8007,$,,2017/08/07,Settlement Date
,,,,,,,,,,2017/08/16,2848.500000
(...)

$ sed  s/\(,,,,,,,,,,[0-9/]*\),[0-9.]*/\1/g  curves.csv
然而,它并没有奏效。它打印出所有行不变。

请帮忙。

1 个答案:

答案 0 :(得分:2)

使用GNU sed的另一种方法:

sed -r '/^,{10}/{s/,[^,]*$//}' file

输出:

(...)
,,,,,,,,,,2017/10/18
,,,,,,,,,,2017/11/15
,usdSN,:usdSN,,,,8005,$,,2017/08/07,Settlement Date
,,,,,,,,,,2017/12/20
,,,,,,,,,,2018/01/17
,,,,,,,,,,2018/02/21
(...)
,,,,,,,,,,2018/12/21
,usdZS,:usdZS,,,,8007,$,,2017/08/07,Settlement Date
,,,,,,,,,,2017/08/16
(...)