AWK novice - calculate the average


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK novice - calculate the average
# 1  
Old 11-04-2010
AWK novice - calculate the average

Hi,
I have the following data in a file for example:
Code:
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 XXXXXXX.1 YYYYYYY.8 ZZZ.1
P2 XXXXXXX.2 YYYYYYY.7 ZZZ.2
P2 XXXXXXX.3 YYYYYYY.2 ZZZ.3
P2 XXXXXXX.4 YYYYYYY.9 ZZZ.4
P2 XXXXXXX.5 YYYYYYY.0 ZZZ.5
P2 XXXXXXX.6 YYYYYYY.7 ZZZ.6
P2 XXXXXXX.7 YYYYYYY.2 ZZZ.7
P2 XXXXXXX.8 YYYYYYY.9 ZZZ.8
P3 XXXXXXX.1 YYYYYYY.8 ZZZ.1
P3 XXXXXXX.2 YYYYYYY.7 ZZZ.2
P3 XXXXXXX.3 YYYYYYY.2 ZZZ.3
P3 XXXXXXX.4 YYYYYYY.9 ZZZ.4
P3 XXXXXXX.5 YYYYYYY.0 ZZZ.5
P4 XXXXXXX.1 YYYYYYY.8 ZZZ.1
P4 XXXXXXX.2 YYYYYYY.7 ZZZ.2
P4 XXXXXXX.3 YYYYYYY.2 ZZZ.3
P4 XXXXXXX.4 YYYYYYY.9 ZZZ.4
P4 XXXXXXX.5 YYYYYYY.0 ZZZ.5
P4 XXXXXXX.6 YYYYYYY.7 ZZZ.6
P4 XXXXXXX.7 YYYYYYY.2 ZZZ.7
P4 XXXXXXX.8 YYYYYYY.9 ZZZ.8

Can you help me with an awk script in order to be able to calculate the coordinate's average for each point? Each point has maximum 8 measurements but can have less. Also the points can be duplicated and some coordinates might have zero values.
If it's possible I would like on the final results to know beside the X Y and Z average, the number of measurements used and if the zero values were used in calculating the average.

Thanks

Last edited by Scott; 11-05-2010 at 07:12 AM.. Reason: Code tags
# 2  
Old 11-05-2010
Post your desire output.
This User Gave Thanks to pravin27 For This Post:
# 3  
Old 11-05-2010
The output should be
Code:
P1 XXXXXXX.Average YYYYYYY.Average ZZZ.Average "number of measurements for P1" "number of measurements with zero value for P1" 
P2 XXXXXXX.Average YYYYYYY.Average ZZZ.Average "number of measurements for P2" "number of measurements with zero value for P2"

Many Thanks

Last edited by Scott; 11-05-2010 at 07:13 AM..
# 4  
Old 11-05-2010
Try this,

Code:
 awk -F"[. ]" 'NR>1 && $1 != p {print p,s"."a/k,x"."b/k,y"."c/k,"number of measurements for",p,"-",k," and number of measurements with zero value for",p," - " z; k=0;a=0;b=0;c=0;z=0}
{if (/\.0/) {++z};a+=$3;b+=$5;c+=$7;++k;p=$1;s=$2;x=$4;y=$6} END {print p,s"."a/k,x"."b/k,y"."c/k,"number of measurements for",p,"-",k," and number of measurements with zero value for",p," - " z}' inputfile


Last edited by pravin27; 11-05-2010 at 07:43 AM..
This User Gave Thanks to pravin27 For This Post:
# 5  
Old 11-05-2010
Code:
awk '{n[$1]++;a[$1]+=$2;b[$1]+=$3;c[$1]+=$4;for (i=2;i<=4;i++){if ($i==0)z[$1]++}}END{for (i in a){print i,a[i]/n[i],b[i]/n[i],c[i]/n[i],"num of 0 val for "i":",(z[$1])?z[i]:0}}' file

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 11-05-2010
Quote:
Originally Posted by bartus11
Code:
awk '{n[$1]++;a[$1]+=$2;b[$1]+=$3;c[$1]+=$4;for (i=2;i<=4;i++){if ($i==0)z[$1]++}}END{for (i in a){print i,a[i]/n[i],b[i]/n[i],c[i]/n[i],"num of 0 val for "i":",(z[$1])?z[i]:0}}' file

I can't get result from your command:

Code:
awk '{n[$1]++;a[$1]+=$2;b[$1]+=$3;c[$1]+=$4;for (i=2;i<=4;i++){if ($i==0)z[$1]++}}END{for (i in a){print i,a[i]/n[i],b[i]/n[i],c[i]/n[i],"num of 0 val for "i":",(z[$1])?z[i]:0}}' infile
P2 0 0 0 num of 0 val for P2: 0
P3 0 0 0 num of 0 val for P3: 0
P4 0 0 0 num of 0 val for P4: 0
P1 0 0 0 num of 0 val for P1: 0

I update from your code, it should be right now.

Code:
awk -F"[ .]" '
 {n[$1]++;a[$1]+=$3;b[$1]+=$5;c[$1]+=$7;for (i=3;i<=7;i=i+2){if ($i==0)z[$1]++}}
 END{for (i in a){print i,$2,a[i]/n[i],$4,b[i]/n[i],$6,c[i]/n[i],n[i],(z[i])?z[i]:"0" |"sort -n"}}
 ' infile

P1 XXXXXXX 4.5 YYYYYYY 4.5 ZZZ 4.5 8 0
P2 XXXXXXX 4.5 YYYYYYY 5.5 ZZZ 4.5 8 1
P3 XXXXXXX 3 YYYYYYY 5.2 ZZZ 3 5 1
P4 XXXXXXX 4.5 YYYYYYY 5.5 ZZZ 4.5 8 1

This User Gave Thanks to rdcwayx For This Post:
# 7  
Old 11-05-2010
Thank you all for all the info's sent.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate the average per block.

My old school way is a one liner. And will search for average from SAR, to get the data receive rate. But, I dont think it is practical or accurate,. Because it calculates to off peak hours. I am planning to change it. My cron runs every 30 mins. When my cron runs, and my time is 14:47pm,, it will... (1 Reply)
Discussion started by: invinzin21
1 Replies

2. Shell Programming and Scripting

Calculate average, azimut and distance

Gents, Please i will to get the distance and azimut from 2 coordinates: Usig excel formula i get the correct values, but i will like to do it using awk. Example A 35089.0 50345.016 9 75 1 2101774 77 70 79 483911.6 2380106.9 137.4 1 1 6 1 A 35089.0 50345.01620 75... (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

Calculate Average time of one column

Hello dears, I have a log file with records like below and want to get a average of one column based on the search of one specific keyword. 2015-02-07 08:15:28 10.102.51.100 10.112.55.101 "kevin.c" POST ... (2 Replies)
Discussion started by: Newman
2 Replies

4. Shell Programming and Scripting

Calculate average for repeated ID within a data

I have an awk script that gives the following output: Average end-to-end transmission delay 2.7 to 5.7 is 0.635392 seconds Average end-to-end transmission delay 2.1 to 5.1 is 0.66272 seconds Average end-to-end transmission delay 2.1 to 5.1 is 0.691712 seconds Average end-to-end transmission... (4 Replies)
Discussion started by: ENG_MOHD
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

Calculate average time using a script

Hello, I'm hoping to get some help on calculating an average time from a list of times (hour:minute:second). Here's what my list looks like right now, it will grow (I can get the full date or change the formatting of this as well): 07:55:31 09:42:00 08:09:02 09:15:23 09:27:45 09:49:26... (4 Replies)
Discussion started by: jaredhanks
4 Replies

7. Shell Programming and Scripting

calculate the average of time series data using AWK

Hi, I have two time series data (below) merged into a file. t1 and t2 are in unit of second I want to calculate the average of V1 every second and count how many times "1" in V2 is occur within a second Input File: t1 V1 t2 V2 10.000000... (5 Replies)
Discussion started by: nica
5 Replies

8. Programming

calculate average

I have a file which is 2 3 4 5 6 6 so i am writing program in C to calculate mean.. #include<stdio.h> #include<string.h> #include <math.h> double CALL mean(int n , double x) main (int argc, char **argv) { char Buf,SEQ; int i; double result = 0; FILE *fp; (3 Replies)
Discussion started by: cdfd123
3 Replies

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

10. UNIX for Dummies Questions & Answers

calculate average of column 2

Hi I have fakebook.csv as following: F1(current date) F2(popularity) F3(name of book) F4(release date of book) 2006-06-21,6860,"Harry Potter",2006-12-31 2006-06-22,,"Harry Potter",2006-12-31 2006-06-23,7120,"Harry Potter",2006-12-31 2006-06-24,,"Harry Potter",2006-12-31... (0 Replies)
Discussion started by: onthetopo
0 Replies
Login or Register to Ask a Question