Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash add up columns with same first column

Tags:

bash

unix

uniq

I have a file that has a name in the first column and count in the second column. It is sorted by name.

    dan 3355
    dan 667
    dan 889
    frank 8
    frank 99
    frank 90
    ian 9

I would like to combine all the same names and output the total count for each name:

    dan 4911
    frank 197
    ian 9

I know that I can use uniq for getting a total count of the identical lines, but how can I preserve the counts that I have in my data?

like image 885
user1190650 Avatar asked Nov 30 '12 17:11

user1190650


2 Answers

You can make use of awk's associative array:

 awk '{arr[$1]+=$2;} END {for (i in arr) print i, arr[i]}' filename
like image 93
P.P Avatar answered Sep 28 '22 16:09

P.P


Using awk's associative memory does not guarantee that names will appear in output in the same order as in input (and may be memory inefficient for large data sets).

Use the following instead

awk '(NR==1){oldname=$1;s=$2;next};
     (oldname == $1){s=s+$2;next};
     {print oldname, s;oldname=$1s=$2;next}
     END{print oldname,s}'
like image 41
Misha Avatar answered Sep 28 '22 14:09

Misha