views:

286

answers:

5

I have a text file which is:

ABC 50
DEF 70
XYZ 20
DEF 100
MNP 60
ABC 30

I want an output which sums up individual values and shows them as a result. For example, total of all ABC values in the file are (50 + 30 = 80) and DEF is (100 + 70 = 170). So the output should sum up all unique 1st column names as -

ABC 80
DEF 170
XYZ 20
MNP 60

Any help will be greatly appreciated.

Thanks

+2  A: 

awk '{sums[$1] += $2} END { for (i in sums) printf("%s %s\n", i, sums[i])}' input_file | sort

if you don't need the results sorted alphabetically, just drop the | sort part.

ggiroux
lose the cat. Its UUOC.
ghostdog74
@ghostdog74 : indeed
ggiroux
+1  A: 
my %data;
while (<>) {
    if (my ($key, $value) = /^(\w+) \s* (\d+)$/x) {
        $data{$key} += $value;
    }
}
printf "%s %s\n", $_, $data{$_} for keys %data;
Leon Timmermans
why not split on white spaces instead of regex?
ghostdog74
Either way would work. My approach is a bit more validating, but also a bit more complex.
Leon Timmermans
+1  A: 
$ awk '{a[$1]+=$2}END{for(i in a) print i,a[i]}' file
ABC 80
XYZ 20
MNP 60
DEF 170
ghostdog74
+5  A: 
$ perl -lane \
    '$sum{$F[0]} += $F[1];
     END { print "$_ $sum{$_}"
             for sort grep length, keys %sum }' \
    input
ABC 80
DEF 170
MNP 60
XYZ 20
Greg Bacon
+2  A: 
perl -lane '$_{$F[0]}+=$F[1]}print"$_ $_{$_}"for keys%_;{' file

And a little bit less straightforward:

perl -ape '$_{$F[0]}+=$F[1]}map$\.="$_ $_{$_}\n",keys%_;{' file
codeholic
3 characters shorter: perl -anE '$_{$F[0]}+=$F[1]}say"$_ $_{$_}"for keys%_;{' file
sid_com
I know. But it works in 5.010 only ;)
codeholic