use Data::Dumper; my ( $key, $value, %hash2 ); while ( ) { $key = $value = undef and next; if /^interface /; $key = $1 if /description\s+ ([^\"][^\s]+)/x; $value = $1 if /switchport trunk allowed vlan\s*(.*)/; next unless $key && $value; $hash2{$key} = $value; $key = $value = undef; } print Dumper \%hash2;