$ perl -MData::Dumper -lanF\\\| -e 'for (@F){@x=split /=/;$h{$x[0]}=$x[1]};$pet_hate{$h{pet}}{HATE}{$h{hate}}++;$pet_hate{$h{pet}}{LIKE}{$h{like}}++; }{print Dumper \%pet_hate' your-file.txt $VAR1 = { '"cow"' => { 'HATE' => { '"rat"' => 3 }, 'LIKE' => { '"dog"' => 3 } }, '"cat"' => { 'LIKE' => { '"horse"' => 1, '"dog"' => 2 }, 'HATE' => { '"rat"' => 3 } } };