sed常見用法,刪除匹配行的上2行,下3行


刪除匹配的下一行到最后一行

[root@test200 ~]# cat test
a
b
c
d
e
f
[root@test200 ~]# sed '/c/{p;:a;N;$!ba;d}' test
a
b
c

定義一個標簽a,匹配c,然后N把下一行加到模式空間里,匹配最后一行時,才退出標簽循環,然后命令d,把這個模式空間里的內容全部清除。

if 匹配"c"
:a
追加下一行
if 不匹配"$"
goto a
最后退出循環,d命令刪除。

刪除匹配行和匹配行后的2行

[root@jiangyi01.sqa.zmf /home/ahao.mah/syslog-ng/muahao_tools]
#cat file
hello muahao
I am good
123 1
this is jack
this is jack
fffff
gg

like this

[root@jiangyi01.sqa.zmf /home/ahao.mah/syslog-ng/muahao_tools]
#sed '/muahao/,+1d' file
123 1
this is jack
this is jack
fffff
gg

like this

[root@jiangyi01.sqa.zmf /home/ahao.mah/syslog-ng/muahao_tools]
#sed '/muahao/,+2d' file
this is jack
this is jack
fffff
gg

like this


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM