views:

35

answers:

4

Hi,

I have a list such as:

10,Car Tyres
8,Car Tyres
4,Wheels
18,Crowbars
5,Jacks
5,Jacks
8,Jacks

The first number is quantity, second is item name. I need to get this list so that it only shows each item once and it adds together the quantity if the item appears more than once. The output of this working correctly would be:

18,Car Tyres
4,Wheels
18,Crowbars
18,Jacks

This will need to work on lists in this format of a few thousand lines, preferably coded in Linux shellscript, any help appreciated, thanks!

A: 

Look at:

man sort
man awk

The actual command you need is:

sort -n -t, +1 yourfile.txt | awk ......

You could also do this entirely in awk Sum by group

James Anderson
Amazing!!!!!!!!
Algorist
i doubt if it really works... i did not see it calculate the sum.and the syntax +0 is obsoleted.
Dyno Fu
A: 
awk -F"," '{   t[$2] = t[$2] + $1    }
END{
    for(o in t){
        print o, t[o]
    }
}' file

output

$ ./shell.sh
Crowbars 18
Wheels 4
Car Tyres 18
Jacks 18
ghostdog74
A: 

How about a perl script?:

#!/usr/bin/perl -w
use strict;

my %parts;

while (<>) {
    chomp;
    my @fields = split /,/, $_;
    if (scalar @fields > 1) {
        if ($parts{$fields[1]}) {
            $parts{$fields[1]} += $fields[0];
        } else {
            $parts{$fields[1]} = $fields[0];
        }
    }
}

foreach my $k (keys %parts) {
    print $parts{$k}, ",$k\n";
}
Paul A Jungwirth
A: 
awk -v FS=, '{ if (! $2 in a) {
                 a[$2] = $1;
               } 
               else {
                 a[$2] += $1;
               } 
             } 
             END { 
               for (name in a) {
                 printf("%s\t%d\n", name, a[name]);
               } 
             }'
Dyno Fu