while (){ #remove spaces from the beginning or the end of the file s/^\s+|\s+$//g; # splits the files based on columns based on space and limit the amount split by 4 my ($col1,$col2,$col3) = split /\s+/,$_,4; #checks to see if the word name is matched to get the variable next over which would be the actual name , then put it in variable $name if ($col1 eq 'name'){ $name = $col2; #checks to see if the word device is matched to get the variable next over which would be the actual type, then next over is another attribute(not on the example) } elsif ($col1 eq 'device') { ##Here the push name, device type and other variable into a hash push @{$hash{$name}},$col2, $col3; } else { # skip line } } close IN; #### foreach my $line(keys %hash) { print $line } #### ##defining the fields you want including the file, HEADER would be the first field and if name or device then KEY is the next value over and VALUE the next use constant { IN_FILE => 'pm_1200636_text.txt', HEADER => 0, KEY => 1, VALUE => 2, }; my %parsed; { open my $fh, '<', IN_FILE; my $name; while (<$fh>) { my @fields = split; if ($fields[HEADER] eq 'name') { $name = $fields[KEY]; next; } #### if ($fields[HEADER] eq 'device') { push @{$parsed{$name}{$fields[KEY]}}, $fields[VALUE]; next; } } }