while(){ if(/port-channel\s+(\d+)/ .. /]\s+\*/){ $1 and $section = $1; $section || next; if(/\s+Ports:/ .. /\n\n/){ /(\S+)\s+(\[\S+\])/ and $data->{$section}{$1} = $2 } } } use Data::Dumper; die Dumper($data) __END__ $VAR1 = { '1' => { 'fc2/5' => '[up]', 'fc1/5' => '[up]' }, '3' => { 'fc1/1' => '[up]' } };