[英]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.