use strict; use warnings; use Data::Dumper; # create top level keys.. my %hash = ( Configuration => '', 'Protection Mode' => '', Databases => {}, Properties => {}, # Fast-Start Failover => '', # it's missing in your output.. 'Configuration Status' => '' ); # track the nasty last line.. my $last_line; # track the current key with subkeys my $current_key; while (){ chomp; # skip empty lines next if /^$/; # remove leaading spaces s/^\s+//; # split on possible separators my @fields = split /\s*\-|:|=\s/,$_; # remove unneeded spaces from fields $fields[0] =~ s/\s+$//; $fields[1] =~ s/\s//g if defined $fields[1]; if ($fields[0] eq 'Configuration Status'){next} # second field present? it is a norma key (no subkeys) if (defined $fields[1]){ $fields[1]=~s/'//g; # some debug with print... print "fields = '$fields[0]' '$fields[1]'\n"; # are we processing a subkey? if ($current_key){ print "\tdefined current key\n"; #print "current -> $current_key - $fields[0]\n"; if (exists $hash{$current_key}){ print "\texists the key '$current_key'\n"; $hash{$current_key}{$fields[0]}=$fields[1]; } } else { # exists the toplevel key? if (exists $hash{$fields[0]}){ $hash{$fields[0]} = $fields[1]; } $current_key = undef; } } # else we are processing subkeys... else{ $current_key = $fields[0]; } $last_line = $fields[0]; if (eof){ $hash{'Configuration Status'} = $last_line; } } print Dumper \%hash; __DATA__ Configuration - DGCONF Protection Mode: MaxAvailability Databases: DB1 - Primary database DB2 - Physical standby database Properties: FastStartFailoverThreshold = '30' OperationTimeout = '30' FastStartFailoverLagLimit = '30' CommunicationTimeout = '180' ObserverReconnect = '0' FastStartFailoverAutoReinstate = 'TRUE' FastStartFailoverPmyShutdown = 'TRUE' BystandersFollowRoleChange = 'ALL' ObserverOverride = 'FALSE' ExternalDestination1 = '' ExternalDestination2 = '' PrimaryLostWriteAction = 'CONTINUE' Fast-Start Failover: DISABLED Configuration Status: SUCCESS