Averaging the rows using 'awk'

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Averaging the rows using 'awk'
# 1  
Old 05-23-2010
Averaging the rows using 'awk'

Dear all,
I have the data in the following format. I want to do average of each NR= 5 (rows) for all the 3 ($1,$2, $3) columns and want to print average result in another file in the same format. I dont know how to write code for this in 'awk', can some one help me to write a code for this in "awk".

   $1                  $2                  $3
   4.000000E+03        4.990302E+00        5.557000E+00
   4.000050E+03        4.998532E+00        5.557000E+00
   4.000100E+03        4.995319E+00        5.557000E+00
   4.000150E+03        5.003360E+00        5.557000E+00
   4.000200E+03        5.000279E+00        5.557000E+00
   4.000250E+03        5.008273E+00        5.557000E+00
   4.000300E+03        5.005232E+00        5.557000E+00
   4.000350E+03        5.013134E+00        5.557000E+00
   4.000400E+03        5.010103E+00        5.557000E+00
   4.000450E+03        5.017992E+00        5.557000E+00
   4.000500E+03        5.014914E+00        5.557000E+00
   4.000550E+03        5.022835E+00        5.557000E+00
   4.000600E+03        5.019675E+00        5.557000E+00
   4.000650E+03        5.027411E+00        5.558000E+00
   4.000700E+03        5.024382E+00        5.558000E+00
   4.000750E+03        5.032073E+00        5.558000E+00
   4.000800E+03        5.029087E+00        5.558000E+00
   4.000850E+03        5.036683E+00        5.558000E+00
..................... ...................... ......................
.................... ....................... ........................

Last edited by Scott; 05-23-2010 at 09:25 AM.. Reason: Please use code tags
# 2  
Old 05-23-2010
Try this:
awk '
function printline() {
  printf("%4s%e", "", s1/5); s1=0
  printf("%8s%e", "", s2/5); s2=0
  printf("%8s%e\n", "", s3/5); s3=0
{s1+=$1;s2+=$2;s3+=$3; n++}
!(NR % 5){ printline() }
  if(s1) printline()
}' file

Last edited by Franklin52; 05-23-2010 at 12:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to perform averaging of values for particular timestamp using awk or anythoing else??

I have a file of the form. 16:00:26,83.33 16:05:26,83.33 16:10:26,83.33 16:15:26,83.33 16:20:26,90.26 16:25:26,83.33 16:30:26,83.33 17:00:26,83.33 17:05:26,83.33 17:10:26,83.33 17:15:26,83.33 17:20:26,90.26 17:25:26,83.33 17:30:26,83.33 For the timestamp 16:00:00 to 16:55:00, I need to... (5 Replies)
Discussion started by: Saidul
5 Replies

2. 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

3. Shell Programming and Scripting

Averaging 3 files with multiple rows

Hi, I am trying to average the values from 3 files with the same format. They are very large files so I will describe the file and show some it of. Basically the file has 83 columns (with nearly 7000 rows). The first three columns are the same for each file while the remaining 80 are values that... (1 Reply)
Discussion started by: kylle345
1 Replies

4. Shell Programming and Scripting

Averaging help in awk

Hi all, I have a data file like below, where Time is in the second column DATE TIME FRAC_DAYS_SINCE_JAN1 2011-06-25 08:03:20.000 175.33564815 2011-06-25 08:03:25.000 175.33570602... (10 Replies)
Discussion started by: gd9629
10 Replies

5. Shell Programming and Scripting

Hourly averaging using Awk

Hey all, I have a set of 5-second data as shown below. I need to find an hourly average of this data. date co2 25/06/2011 08:04:00 8.30 25/06/2011 08:04:05 8.31 25/06/2011 08:04:10 8.32 25/06/2011 08:04:15 8.33 25/06/2011 08:04:20 ... (5 Replies)
Discussion started by: gd9629
5 Replies

6. Shell Programming and Scripting

Averaging data every 30 mins using AWK

A happy Monday to you all, I have a .csv file which contains data taken every 5 seconds. I want to average these 5 second data points into 30 minute averages! date co2 25/06/2011 08:04 8.31 25/06/2011 08:04 8.32 25/06/2011 08:04 8.33... (18 Replies)
Discussion started by: gd9629
18 Replies

7. Shell Programming and Scripting

Averaging in increments using awk & head/tail

Hi, I only have a very limited understanding and experience with writing code and I was hoping I could get some help. I have a dataset of two columns (txt format, numbers in each row separated by a tab) Eg. 1 5 2 5 3 6 4 7 5 6 6 6 7 ... (5 Replies)
Discussion started by: Emred_Skye
5 Replies

8. UNIX for Dummies Questions & Answers


Hello all, I'm trying to perform an averaging procedure which selects a selection of rows, average the corresponding value, selects the next set of rows and average the corresponding values etc. The data below illustrates what I want to do. Given two columns (day and value), I want to... (2 Replies)
Discussion started by: Muhammad Rahiz
2 Replies

9. Shell Programming and Scripting

averaging column values with awk

Hello. Im just starting to learn awk so hang in there with me...I have a large text file formatted as such everything is in a single column ID001 value 1 value 2 value....n ID002 value 1 value 2 value... n I want to be able to calculate the average for values for each ID from the... (18 Replies)
Discussion started by: johnmillsbro
18 Replies

10. Shell Programming and Scripting

AWK - averaging $3 by info in $1

Hello, I have three columns of data of the format below: <name> <volume> <size> a 2 1.2 a 2 1.1 b 3 1.7 c 0.7 1.9 c 0.7 1.9 c 0.7 1.8 What I... (3 Replies)
Discussion started by: itisthus
3 Replies
Login or Register to Ask a Question

Featured Tech Videos