Shell:每秒删除一次与文件中正则表达式的匹配项 [英] Shell: delete every second match against a regex in a file

查看:111
本文介绍了Shell:每秒删除一次与文件中正则表达式的匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想出了一个匹配数据的正则表达式;正则表达式包含2个sed组(包含在()中的子表达式).还要说这个正则表达式重复了9次以匹配整行.我面临的问题是如何(以一种优雅的方式)每秒删除一次与正则表达式的匹配.

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.

推荐答案

假设您有以下字符串,并希望删除出现的bar:

Let's say you have the following string and want to remove the occurrences of bar:

foo bar foo bar foo bar

您可以使用以下sed命令,请注意选项g,该选项使替换次数尽可能多:

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'

输出: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'

输出:foo foo foo foo.

这篇关于Shell:每秒删除一次与文件中正则表达式的匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆