Boolean OR in sed regex

47,933

Solution 1

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'

Solution 2

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'

Solution 3

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.

Solution 4

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
Share:
47,933

Related videos on Youtube

Adam Matan
Author by

Adam Matan

Team leader, developer, and public speaker. I build end-to-end apps using modern cloud infrastructure, especially serverless tools. My current position is R&amp;D Manager at Corvid by Wix.com, a serverless platform for rapid web app generation. My CV and contact details are available on my Github README.

Updated on July 09, 2022

Comments

  • Adam Matan
    Adam Matan almost 2 years

    I'm trying to replace all references of a package named boots in a configuration file.

    The line format is add fast (package OR pkg) boots-(any-other-text), e.g.:

    add fast package boots-2.3
    add fast pkg boots-4.5
    

    I want to replace it with:

    add yinst pkg boots-5.0
    

    I've tried the following sed commands:

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

    What's the right regex? I think I'm missing something in the boolean or (package or pkg) part.

  • Adam Matan
    Adam Matan about 11 years
    sed: invalid option -- E. Doesn't seem to work with GNU sed version 4.1.5 on Red Hat.
  • Spencer Rathbun
    Spencer Rathbun about 11 years
    @AdamMatan Ah, use the -r flag for GNU sed, as Thor indicates.
  • Evgenii Iablokov
    Evgenii Iablokov over 8 years
    It's better to use semi colon in "twice" command. Like this: sed 's/\.ism/\.ism\.md5/g;s/\.usp/\.usp\.md5/g' /tmp/verify
  • bobbogo
    bobbogo over 8 years
    @EvgeniyYablokov Any particular reason? Perhaps in short throwaway scripts (like this one) you may have a point, but I still think the two-line version is easier to read.
  • dgo
    dgo over 7 years
    I'm using the GNU-utils version on Windows @ had to do as @EvgeniyYablokov suggested with the semi-colon for it to work.
  • Walery Strauch
    Walery Strauch over 5 years
    On mac os I got sed: 1: "-E ": invalid command code - using sed -e -E. Switching parameter it worked fine: sed -E -e
  • Ray Foss
    Ray Foss almost 4 years
    brew install gnu-sed && gsed --version... but don't replace it for the whole system as poorly programs that depend on MacOS having a "stable" shell environment will break... also your changes are usually destroyed with ever minor update.