[英]How can I use “sed” to delete 2 lines after match/matches?
I have the following command : sed -i -e '/match1/,+2d' filex
, which deletes 2 lines after finding the match "match1" in the file "file x". 我有以下命令: sed -i -e '/match1/,+2d' filex
,它在文件“file x”中找到匹配“match1”后删除2行。 I want to add several matches to it, like match1, match 2 .... 我想为它添加几个匹配,比如match1,match 2 ....
So it will delete 2 lines after finding any of the matches, how can I achieve this ? 因此在找到任何匹配后会删除2行,我该如何实现?
Two ways, depending upon the sed version and platform: 两种方式,取决于sed版本和平台:
sed -e '/match1/,+2d' -e '/match2/,+2d' < oldfile > newfile
or 要么
sed -e '/match1\|match2/,+2d' < oldfile > newfile
Not a sed user but it seems to me you could use : 不是sed用户,但在我看来你可以使用:
sed -i -e '/(match1|match2)/,+2d' filex
Otherwise you could, if that's possible for you to do, do : 否则你可以,如果你有可能这样做,那么:
sed -i -e '/match1/,+2d' filex && sed -i -e '/match2/,+2d' filex
EDIT: Looks like I had the right idea but ziu got it. 编辑:看起来我有正确的想法,但ziu得到了它。
If I understand you correctly, you want 如果我理解正确,你想要
sed -e '/match1/,+2d' input.txt
For example, create input with seq 10 | sed '3i match1' > input.txt
例如,使用seq 10 | sed '3i match1' > input.txt
创建输入 seq 10 | sed '3i match1' > input.txt
: seq 10 | sed '3i match1' > input.txt
:
1
2
match1
3
4
5
6
7
8
9
10
The output of sed -e '/match1/,+2d' input.txt
would be: sed -e '/match1/,+2d' input.txt
的输出将是:
1
2
5
6
7
8
9
10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.