#! perl -slw use strict; use Data::Dump qw[ pp ]; my( %data, %subhash, $last ); while( ) { if( /^H/ ) { if( $last ) { $data{ $last } = { %subhash }; undef %subhash; } ( $last, my( $key, $val ) ) = m[(\S+)\s+(.+)\s+(\S+)$]; $subhash{ $key } = $val; } else { my( $key, $val ) = m[^\s+(.+)\s+(\S+)$]; $subhash{ $key } = $val; } } $data{ $last } = { %subhash }; pp \%data; __DATA__ Hostname1 1.1.1.1 Cisco Chassis Serial Number xyz123 Interface Gig0/0/31 Hostname2 2.2.2.2 Juniper Chassis Serial Number abc123 Interface Gi-0/0/31