简体   繁体   English

在Bash中将vim replace转换为sed / awk

[英]Convert vim replace to sed / awk in Bash

I want to add a ; 我想添加一个; before every negative value at the end of my data which looks like this: 在数据末尾的每个负值之前,如下所示:

29.01.2019;29.01.2019;KIND;NAME;ITEM;ITEMNUMBER;ITEMORDER;;;;;;;;;;20,00;VAL
29.01.2019;29.01.2019;KIND;NAME;ITEM;ITEMNUMBER;ITEMORDER;012345678;012345678901;FW02ZZZ46847351235;;;;;;;-1,13;;VAL

My trial in vim : 我在vim试用版:

:%s/-\d\{0,5}\,\d\{0,2}/;&\1/g

Unfortunately, I can't call this with sed : 不幸的是,我不能用sed来称呼它:

sed -E 's/-\d\{0,5}\,\d\{0,2}/;&\1/g'

I get the error message: 我收到错误消息:

sed: 1: "s/-\d\{0,5}\,\d\{0,2}/; ...": \1 not defined in the RE

How do I convert this so that I can call it from the command line/with sed ? 如何转换此格式,以便可以从命令行/使用sed进行调用?

Thank you! 谢谢!

You may use 您可以使用

sed -E 's/-\d{0,5}(,\d{1,2})?/;&/g'

Details 细节

  • - - a hyphen -连字符
  • \\d{0,5} - 0 to 5 digits - (,\\d{1,2})? \\d{0,5} -0到5位数字- (,\\d{1,2})? - an optional capturing group matching 1 or 0 occurrences of -匹配1个或0个事件的可选捕获组
    • , - a comma , -逗号
    • \\d{1,2} - 1 or 2 digits. \\d{1,2} -1或2位数字。

The & in the replacement pattern stands for the whole match value. 替换模式中的&代表整个匹配值。

See the online sed demo : 参见在线sed演示

s="29.01.2019;29.01.2019;KIND;NAME;ITEM;ITEMNUMBER;ITEMORDER;;;;;;;;;;20,00;VAL
29.01.2019;29.01.2019;KIND;NAME;ITEM;ITEMNUMBER;ITEMORDER;012345678;012345678901;FW02ZZZ46847351235;;;;;;;-1,13;;VAL"
sed -E 's/-\d{0,5}(,\d{1,2})?/;&/g' <<< "$s"

Output: 输出:

29.01.2019;29.01.2019;KIND;NAME;ITEM;ITEMNUMBER;ITEMORDER;;;;;;;;;;20,00;VAL
29.01.2019;29.01.2019;KIND;NAME;ITEM;ITEMNUMBER;ITEMORDER;012345678;012345678901;FW02ZZZ46847351235;;;;;;;;-1,13;;VAL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM