# warning windows doublequotes perl -lne "BEGIN{$/=''} @arr = split /\s+/,$_;$key=join qq(\n),@arr[0..3]; push @{$hash{$key}},$arr[4]; $arr[4]=~/(\d+):/? $count{$key}+=$1:'';END{print map {qq($_ $count{$_} @{$hash{$_}} \n\n)} keys %hash }" gen1.txt gen2.txt @gi AGAT + AAAA 2 1:1.txt 1:2.txt @gi CATT + AAAAA 3 3:1.txt @gi TACA + AAAA 2 2:2.txt perl -MO=Deparse -lne "BEGIN{$/=''} @arr = split /\s+/,$_;$key=join qq(\n),@arr[0..3]; push @{$hash{$key}},$arr[4]; $arr[4]=~/(\d+):/? $count{$key}+=$1:'';END{print map {qq($_ $count{$_} @{$hash{$_}} \n\n)} keys %hash }" gen1.txt gen2.txt BEGIN { $/ = ""; $\ = "\n"; } LINE: while (defined($_ = )) { chomp $_; sub BEGIN { $/ = ''; } @arr = split(/\s+/, $_, 0); $key = join("\n", @arr[0..3]); push @{$hash{$key};}, $arr[4]; $arr[4] =~ /(\d+):/ ? $count{$key} += $1 : '???'; sub END { print map({"$_ $count{$_} @{$hash{$_};} \n\n";} keys %hash); } ; } -e syntax OK