use strict; use warnings; my $basecol=shift @ARGV or die "WHat base col?"; my %h; $basecol--; # zero based inside program. while (){ chomp; my @f= split ; next unless @f > 1; my $k = $f[$basecol]; $h{$k}{COUNT}++; for my $col(0..$#f){ next if $col==$basecol; $f[$col]=0 unless $f[$col]=~/^\d+$/; $h{$k}{FIELD}[$col]+= $f[$col]; } } for (sort keys %h){ print "$_\t $h{$_}{COUNT}\t"; for my $f(@{ $h{$_}{FIELD} }){ defined $f or $f=''; print "$f\t"; } print "\n"; } __DATA__ U1 ID1 100 280 U1 ID1 137 250 U2 ID2 150 375 U1 ID2 100 100 U3 ID1 100 600 U9 ID3 137 200