Thursday, February 3, 2022

[SOLVED] Deleting a matched line AFTER match another line

Issue

I would like to have your help on this:

I have a file of thousands of lines, I need to find a section of the file and delete some lines in place:

This is the section and the lines I would like to delete are marked:

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
!

This sed would delete the mentioned lines (except the third one), but I need to do it JUST in that section.

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

I would appreciate your help.

Fer

EDIT:

To clarify what I need, if I have this file:

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

Solution

Combine your command into a single group, and address the group with the range you want to affect.

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


Answered By - chepner
Answer Checked By - Robin (WPSolving Admin)