Average in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Average in awk
# 1  
Old 06-19-2008
Average in awk

Hi

I am looking for an awk script which can compute average of all the fields every 5th line. The file looks:
A B C D E F G H I J K L M
1 18 13 14 12 14 13 11 12 12 15 15 15
2 17 17 13 13 13 12 12 11 12 14 15 14
3 16 16 12 12 12 11 11 12 11 16 14 13
4 15 15 11 11 11 12 11 12 11 15 14 16
5 14 14 10 12 11 12 11 13 12 14 16 16
6 13 13 11 11 13 11 10 12 12 14 15 15
7 12 12 27 24 12 12 11 11 15 16 15 14
8 12 11 26 23 11 13 11 12 14 15 15 14
9 12 11 25 22 11 12 11 11 13 14 15 14
10 15 12 24 21 11 12 10 13 13 15 16 14

I need to compute the average of the individual fields (A to M) for every 5th line (line 1 to 5: 6-10 etc).
I greatly appreciate any help.
Thanks
# 2  
Old 06-19-2008
Code:
awk '{
  for (i=1; i<=NF; i++) {
    s+=$i
    c++
    }
  }1    
NR > 1 && !(++_ % 5) {
  printf "avg: %.2f\n", s/c
  s=c=""
  }' file

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 06-19-2008
No better than Radoulov:

Code:
(NR%5)!=0 {
        for (i=1; i<=NF; i++){
                sum[i]+=$i
        }
}
(NR%5)==0 {
        for (i=1; i<=NF; i++) printf "%d ", sum[i]/5
        printf "\n\n"
        delete sum
}

On gawk.
# 4  
Old 06-19-2008
I think I misread the question ...
# 5  
Old 06-19-2008
... or me?
# 6  
Old 06-19-2008
The OP wants
Quote:
the average of the individual fields (A to M)
so your approach seams correct.
I'm not sure, perhaps it should be columns, instead of fileds.

It's compatible with GNU Awk and POSIX Awk (/usr/xpg4/bin/awk on Solaris).

But if the first line contains column headers, you should not use the NR variable, but something like this:

Code:
{
        for (i=1; i<=NF; i++){
                sum[i]+=$i
        }
}
NR>1 && !(++c%5) {
        for (i=1; i<=NF; i++) printf "%d ", sum[i]/5
        printf "\n\n"
        delete sum
}

Or including the input lines (and making it compatible with some nawk versions):

Code:
awk '{ for (i=1; i<=NF; i++) sum[i]+=$i } 1
NR > 1 && !(++_%5) {
        printf "\navg:\n"
        for (i=1; i<=NF; i++) printf "%d ", sum[i]/5
        printf "\n\n"
        split("", sum)
}' file


Last edited by radoulov; 06-19-2008 at 12:24 PM..
# 7  
Old 06-19-2008
thank you all for the help. will try and let you know.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Moving Average

Hi, I'm using awk to try and get a moving average for the second column of numbers ($2) in the below example broken out by unique identifier in column 1 ($1) : H1,1.2 H1,2.3 H1,5.5 H1,6.6 H1,8.7 H1,4.1 H1,6.4 H1,7.8 H1,9.6 H1,3.2 H5,50.1 H5,54.2 H5,58.8 H5,60.9 H5,54.3 H5,52.7... (8 Replies)
Discussion started by: theflamingmoe
8 Replies

2. Shell Programming and Scripting

awk to combine by field and average by another

In the below awk I am trying to combine all matching $4 into a single $5 (up to the -), and count the lines in $6 and average all values in $7. The awk is close but it seems to only be using the last line in the file and skipping all others. The posted input is a sample of the file that is over... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk to average target and gene

I am trying to modify the awk below to include the gene name ($5) for each target and can not seem to do so. Also, I'm not sure the calculation is right (average of all targets that are the same is $4 using the values in $7)? Thank you :). awk '{if((NR>1)&&($4!=last)){printf("%s\t%f\t%s\n",... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Calculating average with awk

I need to find the average from a file like: data => BW:123 M:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1... (4 Replies)
Discussion started by: Slagle
4 Replies

5. Shell Programming and Scripting

Calculate Average AWK

I want to calculate the average line by line of some files with several lines on them, the files are identical, just want to average the 3rd columns of those files.:wall: Example file: File 1 001 0.046 0.667267 001 0.047 0.672028 001 0.048 0.656025 001 0.049 ... (2 Replies)
Discussion started by: AriasFco
2 Replies

6. Shell Programming and Scripting

average of distinct values with awk

Hi guys, I am not an expert in shell and I need help with awk command. I have a file with values like 200 1 1 200 7 2 200 6 3 200 5 4 300 3 1 300 7 2 300 6 3 300 4 4 I need resulting file with averages of... (3 Replies)
Discussion started by: saif
3 Replies

7. Shell Programming and Scripting

AWK novice - calculate the average

Hi, I have the following data in a file for example: P1 XXXXXXX.1 YYYYYYY.1 ZZZ.1 P1 XXXXXXX.2 YYYYYYY.2 ZZZ.2 P1 XXXXXXX.3 YYYYYYY.3 ZZZ.3 P1 XXXXXXX.4 YYYYYYY.4 ZZZ.4 P1 XXXXXXX.5 YYYYYYY.5 ZZZ.5 P1 XXXXXXX.6 YYYYYYY.6 ZZZ.6 P1 XXXXXXX.7 YYYYYYY.7 ZZZ.7 P1 XXXXXXX.8 YYYYYYY.8 ZZZ.8 P2... (6 Replies)
Discussion started by: alex2005
6 Replies

8. UNIX for Dummies Questions & Answers

Average in awk based on time

Hi I am looking for an awk script which can compute the average of the last column based on the date and time. The file looks: site1,"2000-01-01 00:00:00", "2000-01-01 00:59:00",0.013 site2,"2000-02-01 01:00:00", "2000-02-01 01:59:00",0.035 site1,"2000-02-01 02:00:00", "2000-02-01... (15 Replies)
Discussion started by: kathy wang
15 Replies

9. Shell Programming and Scripting

how to average in awk

Hi, I have the data like this $1 $2 1 12 2 13 3 14 4 12 5 12 6 12 7 13 8 14 9 12 10 12 i want to compute average of $1 and $2 every 5th line (1-5 and 6-10) Please help me with awk Thank you (4 Replies)
Discussion started by: saint2006
4 Replies

10. UNIX for Dummies Questions & Answers

Use awk to calculate average of column 3

Suppose I have 500 files in a directory and I need to Use awk to calculate average of column 3 for each of the file, how would I do that? (6 Replies)
Discussion started by: grossgermany
6 Replies
Login or Register to Ask a Question