in reply to Parsing out mulitiple blocks of interesting data in files / ignoring other contents
Hello GeorgMN,
Although use of the range operator in scalar context (i.e., the flip-flop operator) is often useful for this type of problem, in this case you need a flag which can be set and cleared according to more complicated conditions. The following shows one way to do this:
#! perl use strict; use warnings; my @block; my $in_block = 0; while (<DATA>) { if (/^interface/) { push @block, $_; $in_block = 1; } elsif ($in_block) { if (/^!/) { print for (@block, $_); @block = (); $in_block = 0; } elsif (/^\s*shutdown/) { @block = (); $in_block = 0; } else { push @block, $_; } } } __DATA__ # contents of file: ! blah ! blah blah ! # start to grab section here with start (/interface/) interface GigabitEthernet1/1/2 description PC Trunk switchport trunk encapsulation dot1q switchport trunk native vlan 100 switchport mode trunk queue-set 2 priority-queue out mls qos trust dscp storm-control broadcast level 1.00 storm-control multicast level 1.00 spanning-tree guard loop channel-protocol lacp channel-group 1 mode active ! # ^ end section with (/!/) # ignore entire block of data when after (/interface/) a (/shutdown/) +is seen before (/!/)" interface GigabitEthernet1/1/3 shutdown ! interface GigabitEthernet1/1/4 shutdown ! interface TenGigabitEthernet1/1/1 ! interface TenGigabitEthernet1/1/2
Output:
22:16 >perl 1563_SoPW.pl interface GigabitEthernet1/1/2 description PC Trunk switchport trunk encapsulation dot1q switchport trunk native vlan 100 switchport mode trunk queue-set 2 priority-queue out mls qos trust dscp storm-control broadcast level 1.00 storm-control multicast level 1.00 spanning-tree guard loop channel-protocol lacp channel-group 1 mode active ! interface TenGigabitEthernet1/1/1 ! 22:16 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|