in reply to Accumulating Column Total for a Particular Key
#!/usr/bin/perl use strict; use warnings; use Text::CSV; use constant APP => 1; use constant CPU => 2; my $file = $ARGV[0] or die "Usage: $0 <input_file>"; open(my $fh, '<', $file) or die "Unable to open '$file' for reading: $ +!"; <$fh>; # Throw away header my $csv = Text::CSV->new(); my %data; while (<$fh>) { chomp; if ($csv->parse($_)) { my @field = $csv->fields; $data{$field[APP]} += $field[CPU]; } else { warn "parse() failed on '$_'\n"; } } for my $app (keys %data) { print "$app\t$data{$app}\n"; }
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Accumulating Column Total for a Particular Key
by GeneV1 (Initiate) on Aug 27, 2007 at 15:27 UTC | |
by jZed (Prior) on Aug 27, 2007 at 16:54 UTC | |
by GertMT (Hermit) on Aug 28, 2007 at 06:54 UTC |