#!usr/bin/perl use strict; use warnings; use Data::Dumper; my @final; while (<>) { # Read all files that provided through ARGV chomp; my @tmp = split / /, $_; # print Dumper \@tmp; my $first = unpack 'a', $tmp[0]; push @final, join(' ', $first, $tmp[2], $tmp[3]); } continue { close ARGV if eof; # reset $. each file } print Dumper \@final; __END__ $ perl test.pl in.txt $VAR1 = [ 'A 1 violation', 'A 4 violations', 'B 3 violations' ]; __DATA__ A_01: xxxxxxxxxxxxxxxxxxxx......... 1 violation A_02: xxxxxxxxxxxxxxxxxxxx......... 4 violations B_02: xxxxxxxxxxxxxxxxxxxx......... 3 violations #### #!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 #### #!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