1

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.

4
  • You changed your accepted answer :P it's OK, but just a note that you cannot select multiple answer as your accepted answer : ) – αғsнιη Jul 29 '17 at 13:43
  • Both answers are good and helped me complete by bash script and ease up my jos. Thank you – Sir Vic Jul 29 '17 at 13:48
  • 2
    How is this a pivot? Pivot means to change rows to columns. – Barmar Jul 29 '17 at 15:40
  • Question was understood and answered. No point in continuing. My bad for the bad title, but in LibreOffice I used pivot table to aquire the same result. – Sir Vic Jul 29 '17 at 15:44
6

Obligatory GNU Datamash solution

datamash -st '>' groupby 2 sum 1 < data | datamash -t '>' reverse
300>BRIAN
205>DANY
200>NICOLE
4

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

Explanation:

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.