Boolean OR in sed regex

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 09:16:05
sed -e 's/add fast \(pkg\|package\) boots-.*/add yinst pkg boots-5.0/g'

You could always avoid the OR by doing it twice

sed 's/add fast pkg boots-.*/add yinst pkg boots-5.0/g
s/add fast package boots-.*/add yinst pkg boots-5.0/g'

Use extended regex mode, and don't escape the |.

sed -E -e 's/add fast (pkg|package) boots-.*/add yinst pkg boots-5.0/g'

You're mixing BRE's and ERE's either escape both () and | or none.

sed uses basic regular expressions by default, enabling use of extended regular expressions is implementation dependent, e.g. with BSD sed you use the -E switch, GNU sed has it documented as -r, but -E works as well.

GNU (Linux):

1) Make following random strings

   cidr="192.168.1.12"
   cidr="192.168.1.12/32"
   cidr="192.168.1.12,8.8.8.8"

to blank

2) sed with -r for using the logical operators in GNU like @Thor mentioned, and -i to on the fly edit a file on match found

$ echo '<user id="1000" cidr="192.168.1.12">' > /tmp/1000.xml
$ sed -r -i \ 
  s/'cidr="192.168.1.12\/32"|cidr="192.168.1.12"|192.168.1.12,'/''/ /tmp/1000.xml

-r = GNU sed
-i = search / match/ edit the changes to the file on the fly
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!