I have this data:
300>BRIAN
100>DANY
200>NICOLE
105>DANY
And I want to generate the following:
300>BRIAN
205>DANY
200>NICOLE
The delimiter is >
and the first column should SUM.
Obligatory GNU Datamash solution
datamash -st '>' groupby 2 sum 1 < data | datamash -t '>' reverse
300>BRIAN
205>DANY
200>NICOLE
Using awk:
awk -F\> '{pivot[$2]+=$1} END{for (x in pivot) print pivot[x]">"x}' infile.txt
The output is:
300>BRIAN
205>DANY
200>NICOLE
for each uniqe field in column2 suming-up its relative first column value, then at the end, loop over pivot array and first print sum and next their corresponding string.
Using Miller to read the data as a header-less CSV file with >
as the field delimiter, compute the sum of the first field when grouping by the second field. Then reorder the resulting fields so that the new field with the sum is first:
$ mlr --csv --fs '>' -N stats1 -a sum -f 1 -g 2 then reorder -f 1_sum file
300>BRIAN
205>DANY
200>NICOLE
The field named 1_sum
is created by the stats1
operation and contains the sum. This is used in the reorder
operation.