问题
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.
回答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'
回答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'
回答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.
回答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
来源:https://stackoverflow.com/questions/14813145/boolean-or-in-sed-regex