如何匹配和部分替代sed

时间:2015-02-23 12:40:21

标签: regex sed

我怎样才能匹配子串" 2153846-11" (有时仅由数字组成,例如" 2153846",有时喜欢" 2153846-11"或" 2153846_11",有时喜欢" 2153846-1&#34 ;总是数字和第一组中的数字不小于5)以下内容:

"01/16/2015","2153846-11","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"

并将匹配的字符串替换为第一组(在破折号/下划线之前),删除第二组。

最终结果将是:

"01/16/2015","2153846","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"

该指令将写成一个独特的sed行,如

sed -e 's/...//g' < myfile

由于

2 个答案:

答案 0 :(得分:0)

您可以使用此sed:

sed 's/"\([0-9]*\)[_-][0-9]*"/"\1"/g' file
"01/16/2015","2153846","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"

答案 1 :(得分:0)

您可以尝试以下sed命令。

$ echo '"01/16/2015","2153846-11","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"' | sed -r 's/"(2153846)([_-]11)?"/"\1"/g'
"01/16/2015","2153846","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"