[英]Using sed to replace space delimited strings
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
): 这可以按预期工作(使用GNU
sed
):
's/\\(^\\|\\(['\\''" ]\\)\\)bar=[^ ]*/\\2/g; s/[ ][ ]\\+/ /g; s/[ ]*\\(['\\''"]\\+\\)[ ]*/\\1/g'
sed regular expressions are pretty limited. sed正则表达式非常有限。 They don't include \\w as a synonym for [a-zA-Z0-9_], for example.
例如,它们不包含\\ w作为[a-zA-Z0-9_]的同义词。 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...).
它们也不包含\\ b,这意味着单词开头或结尾的零长度字符串(在这种情况下,您确实需要...)。
s/ bar=[^ ]* *//
is close, but the problem is the trailing *
removes the space that might precede the next bar=
. 距离很近,但是问题是结尾
*
删除了下一个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
. 因此,在
... bar=aaa bar=bbb ...
,第一个匹配项是bar=aaa
离开bar=bbb ...
尝试第二个匹配项,但它不匹配,因为您已经消耗了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: 如果您想匹配
bar=something
即使它在字符串的开头,请在开头的开头插入一个空格:
sed 's/^bar=/ bar=/; s/ bar=[^ ]*//'
If you want to remove all instances of bar=something
then you can simplify your regex as such: 如果要删除
bar=something
所有实例,则可以这样简化正则表达式:
\\sbar=\\w+
This matches all bar=
plus all whole words. 这匹配所有
bar=
加上所有整个单词。 The bar=
must be preceded by a whitespace character. 必须在
bar=
之前加一个空格字符。
Demonstration: https://regex101.com/r/xbBhJZ/3 示范: https : //regex101.com/r/xbBhJZ/3
As sed: 如sed:
s/\\sbar=\\w\\+//g
This correctly accounts for foobar=bar. 这正确地说明了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=
. 像Waxrat的答案一样,您必须在开头插入一个空格以使其正确匹配,因为它现在与
bar=
之前的前一个空格字符匹配。 This can be easily done since you're quoting your string explicitly. 因为您要显式引用字符串,所以可以轻松完成此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.