[英]How can I use “sed” to delete 2 lines after match/matches?
我有以下命令: sed -i -e '/match1/,+2d' filex
,它在文件“file x”中找到匹配“match1”后删除2行。 我想为它添加几个匹配,比如match1,match 2 ....
因此在找到任何匹配后会删除2行,我该如何实现?
两种方式,取决于sed版本和平台:
sed -e '/match1/,+2d' -e '/match2/,+2d' < oldfile > newfile
要么
sed -e '/match1\|match2/,+2d' < oldfile > newfile
不是sed用户,但在我看来你可以使用:
sed -i -e '/(match1|match2)/,+2d' filex
否则你可以,如果你有可能这样做,那么:
sed -i -e '/match1/,+2d' filex && sed -i -e '/match2/,+2d' filex
编辑:看起来我有正确的想法,但ziu得到了它。
如果我理解正确,你想要
sed -e '/match1/,+2d' 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
sed -e '/match1/,+2d' input.txt
的输出将是:
1
2
5
6
7
8
9
10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.