use strict; use warnings; use Data::Dumper; my $data = do { local $/; ; }; 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 #### %hash = ( 'ghi.rst-conf' => '21-26,44,47,51', 'def.uvw-conf' => '12-15,31-38', 'abc.xyz-conf' => '15-17,30,35' );