Hi
I have a file like
a,1
b,2
d,3
a,2
b,3
c,7
Result Desired:
a,3
b,5
d,3
c,7
i.e on the bases of 1st field the addition is done of the 2nd field and result printed out.
Thanks
Hi
I have a file like
a,1
b,2
d,3
a,2
b,3
c,7
Result Desired:
a,3
b,5
d,3
c,7
i.e on the bases of 1st field the addition is done of the 2nd field and result printed out.
Thanks
If output order is not a problem this simple perl script will do:
while(<>)
{
chomp;
@a= split(/,/);
$sum{$a[0]} += $a[1];
}
foreach $key (keys %sum)
{
print "$key,$sum{$key}\n";
}
awk 'BEGIN{FS=","}
{ array[$1]+=$2 }
END{
for (i in array) print i,array
}' "file"
If order is an issue
while(<>)
{
chomp;
@a= split(/,/);
push (@order,$a[0]) if (!$sum{$a[0]});
$sum{$a[0]} += $a[1];
}
foreach $key (@order)
{
print "$key,$sum{$key}\n";
}