in reply to Another way to match multi-lines from config.

I'm not quite sure from your post exactly what it is you want to do. I'm guessing that there are multiple "interface" descriptions in the configuration file and you need to extract one key/value pair from each. Processing line by line is probably not a good idea as you could get an interface with a good description line but a bad switchport line followed by the converse, thus ending up with a key from the one associated with a value from the other.

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