简体   繁体   English

使用带有sed的正则表达式用下划线替换空格

[英]Use regex with sed to replace spaces by underscore

I need to replace all the matches of the following PCRE by underscores in a file with sed. 我需要用sed文件中的下划线替换以下PCRE的所有匹配项。

/(?:^Node-path: |\G)\S+\K\h+/gm

I have tried escaping parenthesis, remove espacing of G, S, K... but still can't get it to work. 我已经尝试了转义括号,删除了G,S,K ...的转义,但是仍然无法使其正常工作。

Anyone can help plase? 有人可以帮忙吗?

If your sed is gnu sed, you can mix NUMBER and g modifier in order to skip the first space: 如果您的sed是gnu sed,则可以混合使用NUMBERg修饰符以跳过第一个空格:

sed '/^Node-path:/s/ /_/2g'

If your sed is not gnu sed, the NUMBER+g may not be supported. 如果未锁定您的sed,则可能不支持NUMBER+g However you can do this trick: 但是,您可以执行以下操作:

sed '/^Node-path:/{s/ /_/g;s/_/ /}'

or 要么

sed '/^Node-path:/{s/ /_/g;s/:_/: /}'

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

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