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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please Help!!!! Awk for summing columns based on selected column value
# 1  
Old 10-15-2012
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!!!!!!!
# 2  
Old 10-15-2012
Please provide your sample input and desired output.
# 3  
Old 10-15-2012
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.

Last edited by BrownBob; 10-15-2012 at 08:19 AM..
# 4  
Old 10-15-2012
Code:
awk -F, '{tot1[$9]+=$22;tot2[$9]+=$23}
         END    {print "name: total1 total2 total";
                 for (i in tot2) print i, tot1[i], tot2[i], tot1[i]+tot2[i]
                }
        ' file

This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-15-2012
Quote:
Originally Posted by BrownBob
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.
You can use associative arrays like below ,

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

This User Gave Thanks to codemaniac For This Post:
# 6  
Old 10-15-2012
Quote:
Originally Posted by RudiC
Code:
awk -F, '{tot1[$9]+=$22;tot2[$9]+=$23}
         END    {print "name: total1 total2 total";
                 for (i in tot2) print i, tot1[i], tot2[i], tot1[i]+tot2[i]
                }
        ' file

This final - total1[i]+total2[i] for all $22 and $23 is required at the of all the records and not individual records.
# 7  
Old 10-15-2012
Code:
awk -F, '{tot1[$9]+=$22;tot2[$9]+=$23}
         END    {print "name: total1 total2";
                 for (i in tot2) {print i, tot1[i], tot2[i];total1+=tot1[i]; total2+=tot2[i]}
                 print "total",total1, total2;
                }
        ' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is it possible to ascend a numbers based on selected columns?

I have to ascend the number of two selected columns by horizontal manner. For example, I have a data like this in csv file (tab delimited format) 08 1 19185 18010 16 4 7960 9339 01 6 516769 517428 09 9 51384 49270 I need to ascend the two columns numbers (horizontal manner) like as... (5 Replies)
Discussion started by: dineshkumarsrk
5 Replies

2. Shell Programming and Scripting

Combine multiple rows based on selected column keys

Hello I want to collapse a file with multiple rows into consolidated lines of entries based on selected columns as the 'key'. Example: 1 2 3 Abc def ghi 1 2 3 jkl mno p qrts 6 9 0 mno def Abc 7 8 4 Abc mno mno abc 7 8 9 mno mno abc 7 8 9 mno j k So if columns 1, 2 and 3 are... (6 Replies)
Discussion started by: linuxlearner123
6 Replies

3. UNIX for Advanced & Expert Users

Conversion of rows to columns using awk based om column value

HI, My Input file data is dn:adcfgeneral id:13343 Name:xxxxxx Password:iutyerwuitywue wpuwt tuiytruityrutyrwtyrwp dn:cdferwjyyyy id:3875 Name:yyyy Password :hgfdsjkfhdsfkdlshf dshfkldshfdklsfh interset:uiuiufj My output should be ... (6 Replies)
Discussion started by: dineshaila
6 Replies

4. Shell Programming and Scripting

awk split columns after matching on rows and summing the last column

input: chr1 1 2 3 chr1 1 2 4 chr1 2 4 5 chr2 3 6 9 chr2 3 6 10 Code: awk '{a+=$4}END{for (i in a) print i,a}' input Output: chr112 7 chr236 19 chr124 5 Desired output: chr1 1 2 7 chr2 3 6 19 chr1 2 4 5 (1 Reply)
Discussion started by: jacobs.smith
1 Replies

5. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

displaying columns based on column name

Hello, I have a huge file with many columns . I want to use the names of the columns to print columns to another file. for example file.txt COLA COLB COLC COLD 1 2 3 5 3 5 6 9 If I give name for multiple columns to the code:... (5 Replies)
Discussion started by: ryan9011
5 Replies

7. Shell Programming and Scripting

Awk to add selected row column data

Looks at the most efficient way to add up the column of data based off of the rows. Random data Name-Number-ID Sarah-2.0-15 Bob-6.3-15 Sally-1.0-10 James-1.0-10 Scotty-10.7-15 So I would select all those who have ID = 15 and then add up total number read - p "Enter ID number" Num ... (3 Replies)
Discussion started by: Ironguru
3 Replies

8. Shell Programming and Scripting

shell script(Preferably awk or sed) to print selected number of columns from each row

Hi Experts, The question may look very silly by seeing the title, but please have a look at it clearly. I have a text file where the first 5 columns in each row were supposed to be attributes of a sample(like sample name, number, status etc) and the next 25 columns are parameters on which... (3 Replies)
Discussion started by: ks_reddy
3 Replies

9. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies

10. UNIX for Advanced & Expert Users

Grep all the columns based on a particular column

This is the data file that I have 21879, 0, 22, 58, 388 0, -1, 300, 1219172589765, 1708, 0, 200, 21891, 0, 0, 33, 309 0, -1, 300, 1219172591478, 1768, 0, 200, 22505, 0, 0, 33, 339 0, -1, 300, 1219172593251, 1738, 0, 200, 21888, 0, 1, 33, 308 0, -1, 300, 1219172594995, 633, 0, 200, 24878,... (2 Replies)
Discussion started by: pmallur
2 Replies
Login or Register to Ask a Question