#!usr/bin/perl use strict; use warnings; use Data::Dumper; my $num_viol = 'number of violation(s)'; sub extract_data { my (@lines) = @_; my %hash; foreach my $line (@lines) { my @tmp = split / /, $line; my $first = unpack 'a', $tmp[0]; if (exists $hash{$first}) { $hash{$first}{$num_viol} += $tmp[2]; next; } $hash{$first}{$num_viol} = int($tmp[2]); } return \%hash; } my @lines; my $concatenated; while (<>) { # Read all files that provided through ARGV chomp; if ($_ =~ /^\s*$/) { $. = 0; next; } elsif ($. == 1) { $concatenated .= $_; } elsif ($. == 3) { $concatenated .= $_; push @lines, $concatenated; $. = 0; $concatenated = ''; } } continue { close ARGV if eof; # reset $. each file } my $hash = extract_data(@lines); 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: xxxxxxx xxxxxxxxxx xxx......... 1 violation A_02: xxxxxxx xxxxxxxxxx xxx......... 4 violations B_02: xxxxxxx xxxxxxxxxx xxx......... 3 violations