Loop through multiple rows using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through multiple rows using awk
# 8  
Old 12-15-2011
hi again, I' m getting syntax error when i tried to incorporate your code to my messy script. can u please take a look what i am doing wrong. thanks.

Code:
for i in $infiles; do

awk 'BEGIN{FS=OFS=" "}
{$1=$9=$10=$11=$12=$13=$14=$15="";  gsub(FS"+",FS)}1' $i \ 
| awk '!/-999.00/' | awk 'NR == 1; NR > 1 {print $0 | "sort -k1"}' \
| awk '{a[$1":"$4]+=$7 c[$1":"$3]++} end {for (j in a){split (j,_,":") printf ("%d\t%d\t%f\n",_[1],_[3],a[j]/c[j])}}' > "$i".ave

done

I just modify the corresponding column numbers to correspond to the target fields i needed to average in my actual data (with header shown below).

Code:
record year month day lon lat wind

thanks again.

Last edited by ida1215; 12-15-2011 at 06:23 AM..
# 9  
Old 12-15-2011
Code:
[root@tmp]cat input_file
1          1         2.3
1          1         1.5
1          2         4.0
1          2         1.5
2          1         2.0
2          1         1.6
 
[root@tmp]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
 
1       1       1.900000
1       2       2.750000
2       1       1.800000

If you want to put it in online, this is how it should be... you were missing some semicolon
Code:
awk '/^[0-9]/{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
This User Gave Thanks to ahamed101 For This Post:
# 10  
Old 12-15-2011
Hi ahamed, thanks much, its now running.
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