#!usr/bin/perl use strict; use warnings; use Data::Dumper; my $num_viol = 'number of violation(s)'; my %hash; while (<>) { # Read all files that provided through ARGV chomp; my @tmp = split / /, $_; my $first = unpack 'a', $tmp[0]; if (exists $hash{$first}) { $hash{$first}{$num_viol} += $tmp[2]; next; } $hash{$first}{$num_viol} = $tmp[2]; } continue { close ARGV if eof; # reset $. each file } my @final; foreach my $violation (sort keys %hash) { push @final, join(' ', $violation, $num_viol, $hash{$violation}{'number of violation(s)'}); } print Dumper \@final; __END__ $ perl test.pl in.txt $VAR1 = [ 'A number of violation(s) 5', 'B number of violation(s) 3' ]; __DATA__ A_01: xxxxxxxxxxxxxxxxxxxx......... 1 violation A_02: xxxxxxxxxxxxxxxxxxxx......... 4 violations B_02: xxxxxxxxxxxxxxxxxxxx......... 3 violations