首页 > 解决方案 > 在匹配另一行之后删除匹配的行

问题描述

我想得到你的帮助:

我有一个数千行的文件,我需要找到文件的一部分并在适当的位置删除一些行:

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

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

我会很感激你的帮助。

费尔

编辑:

为了澄清我需要什么,如果我有这个文件:

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

标签: bashsed

解决方案


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

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

推荐阅读