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

转载于:https://www.cnblogs.com/muahao/p/6290813.html

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值