Loop through multiple rows using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through multiple rows using awk
# 1  
Old 12-15-2011
Loop through multiple rows using awk

Hi, i'm been browsing through the threads on how to do looping of multiple lines in awk but havent found the one i needed. I have a data which looks like this below. I need to compute for the monthly average of values per record and i used the awk argument below. how do i tell awk to execute the averaging on rows with similar record number?

Code:
awk 'NR > 1{next} { cnt[$3]++; tot1[$3]+=$5; tot2[$3]+=$6; tot3[$3]+=$7}END \
{ for (j in tot1) { print j, tot1[j]/cnt[j], tot2[j]/cnt[j], tot3[j]/cnt[j]}}'

Code:
record month value
1          1         2.3  
1          1         1.5
1          2         4.0
1          2         1.5
2          1         2.0
2          1         1.6

Thanks in advance.

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 12-15-2011 at 03:25 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 12-15-2011
Didn't quite understand... Can you post the expected output?

--ahamed
# 3  
Old 12-15-2011
I'm sorry for the confusion. I need an output like the ones below:

Record Month Values
1 1 mean for month 1
2 mean for month 2
n mean for month 3
2 1 mean for month 1
2 mean for month 2
n mean for month n

thanks much.
# 4  
Old 12-15-2011
You don't need to read the next line manually - awk will automatically process the whole input file line by line.

Your field numbers seem wrong if that's your real data - there are only 3 fields.
# 5  
Old 12-15-2011
So are you looking for an ouput like this?

Code:
record month avg
1   1   1.9
1   2   2.75
2   1   1.8

--ahamed
# 6  
Old 12-15-2011
Yes that's what i hope to output. sorry i am really bad at scripting.
# 7  
Old 12-15-2011
Try this...
Code:
awk '{
  a[$1":"$2]+=$3
  c[$1":"$2]++
}
END{
  for(i in a){
    split(i,_,":")
    printf("%d\t%d\t%f\n",_[1],_[2],a[i]/c[i])
  }
}' input_file

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare values in multiple rows in one column using awk

I would like to compare values in column 8, and grep the ones where the different is > 1, columns 1 and 2 are the key for array. Every 4 rows the records values in columns 1 and 2 changed. Then, the comparison in the column 8 need to be done for the 4 rows everytime columns 1 and 2 changed ... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

awk to ignore multiple rows based on a condition

All, I have a text file(Inputfile.csv) with millions of rows and 100 columns. Check the sample for 2 columns below. Key,Check A,1 A,2 A, A,4 B,0 B,1 B,2 B,3 B,4 .... million rows. My requirement is to delete all the rows corresponding to all the keys which ever has at least one... (4 Replies)
Discussion started by: ks_reddy
4 Replies

3. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

4. Shell Programming and Scripting

Using multiple gsub() function under a loop in awk

Hi ALL, I want to replace string occurrence in my file "Config" using a external file named "Mapping" using awk. $cat Config ! Configuration file for RAVI ! Configuration file for RACHANA ! Configuration file for BALLU $cat Mapping ravi:ram rachana:shyam ballu:hameed The... (5 Replies)
Discussion started by: useless79
5 Replies

5. Shell Programming and Scripting

FOR loop with multiple files as input and awk

Hi all , i want to pass multiple files as input to a for loop for i in file1 file2 file3 do some awk action < $i >> $i.out done but im getting error in that for loop is the way i use to pass files to awk using for correct and 2.we can directly pass multiple files to awk as... (7 Replies)
Discussion started by: zozoo
7 Replies

6. Shell Programming and Scripting

awk to grep rows by multiple fields

Hello, I met a challenge to extract part of the table. I'd like to grep the first three matches based on field1 and field2. Input: D A 92.85 1315 83 11 D A 95.90 757 28 3 D A 94.38 480 20 7 D A 91.21 307 21 6 D A 94.26 244 ... (6 Replies)
Discussion started by: yifangt
6 Replies

7. Shell Programming and Scripting

Loop for row-wise averaging of multiple files using awk

Hello all, I need to compute a row-wise average of files with a single column based on the pattern of the filenames. I really appreciate any help on this. it would just be very difficult to do them manually as the rows are mounting to 100,000 lines. the filenames are as below with convention as... (2 Replies)
Discussion started by: ida1215
2 Replies

8. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

9. Shell Programming and Scripting

Split single rows to multiple rows ..

Hi pls help me out to short out this problem rm PAB113_011.out rm: PAB113_011.out: override protection 644 (yes/no)? n If i give y it remove the file. But i added the rm command as a part of ksh file and i tried to remove the file. Its not removing and the the file prompting as... (7 Replies)
Discussion started by: sri_aue
7 Replies

10. Shell Programming and Scripting

extract multiple cloumns from multiple files; skip rows and include filenames; awk

Hello, I am trying to write a bash shell script that does the following: 1.Finds all *.txt files within my directory of interest 2. reads each of the files (25 files) one by one (tab-delimited format and have the same data format) 3. skips the first 10 rows of the file 4. extracts and... (4 Replies)
Discussion started by: manishabh
4 Replies
Login or Register to Ask a Question