Recursive text search whithin lines before the gre

2019-09-02 11:01发布

问题:

I need help working with router config backup database. I need to get a list of interfaces that don't have vrf or shutdown in their configuration. I get the list of all interfaces config passing the config file through awk '/^interface/,/!/'. This gives me the output below:

interface TenGigE0/3/0/0
 description 
 service-policy output QOS
 ipv4 mtu 1500
 ipv4 address 13.24.15.3 255.255.255.252
 carrier-delay up 3000 down 0
 load-interval 30
 dampening
!
interface TenGigE0/3/0/1
 description Link To 
!
interface TenGigE0/3/0/1.302
 description 
 vrf 1671
 ipv4 address 13.24.14.11 255.255.255.254
 encapsulation dot1q 302

Now, i am stuck trying to exclude the interfaces that contain vrf line. What i was trying to do is to grep for vrf, and when there is a match, remove the line that contains the word "interface" above. Unfortunately with no luck. Maybe someone has a more sophisticated solution.

回答1:

If you have the structured records awk can solve this problem. Given your intermediate file

2$ awk 'BEGIN{RS=ORS="!\n"} !/vrf/' interface

will print the records without "vrf"

interface TenGigE0/3/0/0
 description
 service-policy output QOS
 ipv4 mtu 1500
 ipv4 address 13.24.15.3 255.255.255.252
 carrier-delay up 3000 down 0
 load-interval 30
 dampening
!
interface TenGigE0/3/0/1
 description Link To
!