简体   繁体   中英

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.

/(?:^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.

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 '/^Node-path:/s/ /_/2g'

If your sed is not gnu sed, the NUMBER+g may not be supported. However you can do this trick:

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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