in reply to Perl Sum & Count column based data
This will let you specify a "Base" column as the first argument on the command line, followed by any number of other columns to count and sum (the first column is numbered zero, as in your example):
abaugher@cail> cat 1062685.pl #!/usr/bin/env perl use strict; use warnings; my $key = shift; my @cols = @ARGV; my %h; die "Usage: 1062685.pl id_column field1 [field2]...\n" unless @cols; # $ARGV[0] is the Base column # $ARGV[1..x] is the list of columns to add up while(<DATA>){ chomp; my @f = split; for (@cols){ $h{$f[$key]}{$_}{t} += $f[$_]; $h{$f[$key]}{$_}{n}++; } } for my $k (sort keys %h){ print $k; print "\t$h{$k}{$_}{n}\t$h{$k}{$_}{t}" for @cols; 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 abaugher@cail> perl 1062685.pl 1 2 3 ID1 3 337 3 1130 ID2 2 250 2 475 ID3 1 137 1 200 abaugher@cail> perl 1062685.pl 0 2 3 U1 3 337 3 630 U2 1 150 1 375 U3 1 100 1 600 U9 1 137 1 200
Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.
|
|---|