Awk help needed to calculate total

Hi all,

I have a flat file like

10 steven
25 mike
47 Charles
127 Nancy
34 steven
23 mike
67 Charles
7761 Nancy
8 steven
54 mike
88 Charles
1267 Nancy

I need to calculate the total of steven and all the members , for this I am using like

grep "`sed -n 1p patterns.txt`" temp.log | awk '{x+=$1}END{print "Total "x}'

where patterns.txt contains
steven
mike
Charles
Nancy

I think i'm using wrong method, is there any other method is available which can do this job very easily.

Regards,
senthilkumar ak

#  awk '{t[$2]+=$1}END{for (i in t){print i,t}}' file
Nancy 9155
Charles 202
steven 52
mike 102

you can't get simpler than this :b:
great work..

first sort them by the second field.
then do

nawk 'NR==1{v=$2;Total=0}$1!=v{print v" Total=" Total} ;Total=0;v=$2}$2==v{Total=Total+$1}' input > Total

This should do it.

Regards

Dear Tytalus,

how does this code know the value of i in the END braces.. is it by default ?

Regards

here t is a array so i reads from array t till it reaches the end..
so i don't think you need a value for i..
its called iterating over all the indices of an array..
:slight_smile:

Thank you very very much.. :slight_smile:

Thanks Tytalus and 'yanyaaa', A little more help from you.. is there any way thart the output is of the awk command display without sorting it ..?

Senthilkumar ak