Please Help!!!! Awk for summing columns based on selected column value

a,b,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,bb,cc,dd,ee,ff,gg,hh,ii

a thru ii are digits and strings....

The awk needed....if coloumn 9 == i (coloumn 9 is string [name]), output the sum of x's(coloumn 22 [marks in maths]) in all records and sum of y's (coloumn 23 [marks in molecular biology]) in all records in a file (records.txt).

Please help!!!!!!!

Please provide your sample input and desired output.

aa,706287,96,990,62.1,62,1,10,Shelly,0x,24,,mSorN,1,,1,2,"2012-10-08",0x,3563005,530,71,929,,abno,"2012-10-08",,766629788,GER,C:23031:14162,1651,201210
aa,6055,9,01284,62.1,62,1,10,Shelly,0x,22,P,,mSorN,1,,1,2,"2012-10-08",08x,3448766,26041,33520,1800,,time,"2012-10-08",15,766629790,UTR,S:31502:17323,1931,201210

name: total1 total2
Shelly 26112 34449

Also would need a final shelly total= total1+total2 (a single total for all the records in the file at the end) .. we get the total1 by adding all coloumn 22 values in all lines, and total 2 by adding all coloumn values in coloumn 23 in all lines of the file.

awk -F, '{tot1[$9]+=$22;tot2[$9]+=$23}
         END    {print "name: total1 total2 total";
                 for (i in tot2) print i, tot1, tot2, tot1+tot2
                }
        ' file
1 Like

You can use associative arrays like below ,

awk -F"," '{math[$9]+=$22;}END{for (i in math) {print i,math}}' sampleInput.txt
1 Like

This final - total1[i]+total2 [i]for all $22 and $23 is required at the of all the records and not individual records.

awk -F, '{tot1[$9]+=$22;tot2[$9]+=$23}
         END    {print "name: total1 total2";
                 for (i in tot2) {print i, tot1, tot2;total1+=tot1; total2+=tot2}
                 print "total",total1, total2;
                }
        ' file