Say I have come up with a regex matching a piece of data; the regex contains 2 sed
groups (sub-expressions enclosed in (
and )
). Also say that this regex is duplicated 9 times to match a whole line. The problem I am facing is how to delete (in an elegant way) every second match against the regex.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Let's say you have the following string and want to remove the occurrences of bar
:
foo bar foo bar foo bar
You can use the following sed
command, note the option g
which makes the substitution happen as many times as possible:
sed -r 's/([a-z]+) ([a-z]+)/\1/g' <<< 'foo bar foo bar foo bar'
Output: foo foo foo
.
However this would not work with a string where the number of words is not even. I would make the second capturing group optional using the *
quantifier to make the above commmand even work with such strings:
sed -r 's/([a-z]+) ([a-z]+)*/\1/g' <<< 'foo bar foo bar foo bar foo'
Output: foo foo foo foo
.