匹配另一行后删除匹配的行

时间:2020-01-29 16:56:51

标签: bash sed

我想在此方面寻求您的帮助:

我有成千上万行的文件,我需要找到文件的一部分并删除一些行:

这是此部分,我要删除的行已标记:

interface Vlan824
 description WRES_824
 vrf forwarding V211
 ip address 172.17.224.2 255.255.240.0 #### Delete this ####
 ip helper-address xxxx
 ip helper-address xxxy
 no ip redirects
 no ip proxy-arp
 ip verify unicast source reachable-via rx 2699
 standby delay minimum 0 reload 60
 standby version 2
 standby 0 ip 172.17.224.1 #### Delete this ####
 standby 0 priority 110
 standby 0 preempt delay minimum 300 reload 300
 shutdown
 standby 1 ipv6 FE80::1 #### Delete this ####
 standby 1 ipv6 <IPV6-PREFIX-1>0E:824::1/64 #### Delete this ####
 standby 1 priority 110
 standby 1 preempt delay minimum 300 reload 300
 ipv6 address FE80::2 link-local #### Delete this #### 
 ipv6 address <IPV6-PREFIX-1>0E:824::2/64 #### Delete this ####
 ipv6 nd prefix <IPV6-PREFIX-1>0E:824::/64 no-advertise #### Delete this ####
 ipv6 nd managed-config-flag
 ipv6 nd other-config-flag
 no ipv6 redirects
 ipv6 dhcp relay destination xxx
 ipv6 dhcp relay destination xxx
 ipv6 verify unicast source reachable-via rx URPF
 bfd interval 750 min_rx 750 multiplier 3
 arp timeout 300
!

此sed会删除提及的行(第三行除外),但我只需要在该部分中进行。

sed -i '/224.1/d; /224.2/d; /224.3/d; /:224::/d; /:824::/d' FILE.txt

感谢您的帮助。

Fer

编辑:

要弄清楚我需要什么,如果我有此文件:

aaa
bbb
hhh
eeb
ccc
!
aab     I need to find this section ( from aab to ggc ) 
hhb     and delete just the eeb line
eeb
ffb
ggc
!
aac
hhc
eeb
ffc

2 个答案:

答案 0 :(得分:0)

假设您要编辑的部分是Vlan824(以!字符结尾),匹配删除行的模式为224.1和FE80

sed -n '/Vlan824/,//p;/^\!/q' your-file | grep -v '224.1\|FE80'

答案 1 :(得分:0)

将您的命令合并为一个组,然后使用您要影响的范围来寻址该组。

sed -i '/interface Vlan824/,/!/{/224.1/d;/224.2/d;/224.3/d;/:224::/d;/:824::/d;}' foo.txt