开发者

Sorting List and Adding Together Amounts Shellscript

开发者 https://www.devze.com 2022-12-17 02:20 出处:网络
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 g开发者_如何学Pythonet this list so that it

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 g开发者_如何学Pythonet 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!


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


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";
}


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]);
               } 
             }'


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

0

精彩评论

暂无评论...
验证码 换一张
取 消