echo 'bar=start "bar=second CONFIG="$CONFIG bar=s buz=zar bar=g bar=ggg bar=f bar=foo bar=zoo really?=yes bar=z bar=yes bar=y bar=one bar=o que=idn"' | sed -e 's/^\\|\\([ "]\\)bar=[^ ]*[ ]*/\\1/g'
Actual output:
CONFIG="$CONFIG buz=zar bar=ggg bar=foo really?=yes bar=yes bar=one que=idn"
Expected output:
CONFIG="$CONFIG buz=zar really?=yes que=idn"
What I'm missing in my regex?
Edit:
This works as expected (with GNU sed
):
's/\\(^\\|\\(['\\''" ]\\)\\)bar=[^ ]*/\\2/g; s/[ ][ ]\\+/ /g; s/[ ]*\\(['\\''"]\\+\\)[ ]*/\\1/g'
sed regular expressions are pretty limited. They don't include \\w as a synonym for [a-zA-Z0-9_], for example. They also don't include \\b which means the zero-length string at the beginning or end of a word (which you really want in this situation...).
s/ bar=[^ ]* *//
is close, but the problem is the trailing *
removes the space that might precede the next bar=
. So, in ... bar=aaa bar=bbb ...
the first match is bar=aaa
leaving bar=bbb ...
to try for the second match but it won't match because you already consumed the space before bar
.
s/ bar=[^ ]*//
is better -- don't consume the trailing spaces, leave them for the next match attempt. If you want to match bar=something
even if it's at the beginning of the string, insert a space at the beginning first:
sed 's/^bar=/ bar=/; s/ bar=[^ ]*//'
If you want to remove all instances of bar=something
then you can simplify your regex as such:
\\sbar=\\w+
This matches all bar=
plus all whole words. The bar=
must be preceded by a whitespace character.
Demonstration: https://regex101.com/r/xbBhJZ/3
As sed:
s/\\sbar=\\w\\+//g
This correctly accounts for foobar=bar.
Like Waxrat's answer, you have to insert a space at the beginning for it to properly match as it's now matching against a preceding whitespace character before the bar=
. This can be easily done since you're quoting your string explicitly.
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.