#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use List::Util qw{ sum }; my %h; $/ = q(); while (my $block = <>) { my @lines = split /\n/, $block; my $key = $lines[1]; my ($count) = $lines[3] =~ /\s(\d+)/; unless (exists $h{$key}) { $block =~ s/\n\n?$//; $block =~ s/\s*\d+$//; $h{$key} = $block; } $h{$key} .= "\t$count"; } for my $key (sort keys %h) { my ($match) = $h{$key} =~ /((?:\d+\t*)+)$/; my @counts = $match =~ /\d+/g; my $sum = sum(@counts); say join "\t", $h{$key}, "count:$sum\n"; }