As long as the configuration file was of manageable size I would read the whole file into a string and then split it into individual interfaces using a look-ahead pattern. Then I would iterate over the interfaces extracting both key and value using a single regex and adding them to the hash.
use strict; use warnings; use Data::Dumper; my $data = do { local $/; <DATA>; }; my @interfaces = split m{(?=interface)}, $data; my $rxExtract = qr {(?xms) description\s+ (\S+) .*? switchport\strunk\sallowed\svlan\s* ([^\n]+) }; my %hash; foreach my $interface ( @interfaces ) { $hash{$1} = $2 if $interface =~ $rxExtract } print Data::Dumper->Dumpxs([\%hash], [q{*hash}]); __END__ interface Port-channel31 description abc.xyz-conf no ip address load-interval 60 switchport switchport trunk encapsulation dot1q switchport trunk allowed vlan 15-17,30,35 interface Port-channel32 description def.uvw-conf no ip address load-interval 55 switchport switchport trunk encapsulation dot1q switchport trunk allowed vlan 12-15,31-38 interface Port-channel35 description ghi.rst-conf no ip address load-interval 75 switchport switchport trunk encapsulation dot1q switchport trunk allowed vlan 21-26,44,47,51
Here's the Data::Dumper output.
%hash = ( 'ghi.rst-conf' => '21-26,44,47,51', 'def.uvw-conf' => '12-15,31-38', 'abc.xyz-conf' => '15-17,30,35' );
As I said, I'm not sure if this is what you want but, hopefully, it will give you some help.
Cheers,
JohnGG
In reply to Re: Another way to match multi-lines from config.
by johngg
in thread Another way to match multi-lines from config.
by nidhi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |