Boolean OR in sed regex

Adam Matan picture Adam Matan · Feb 11, 2013 · Viewed 38.5k times · Source

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 fast 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.

Answer

bobbogo picture bobbogo · Feb 11, 2013
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'