Calculate average of each of position of contents in a huge file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate average of each of position of contents in a huge file
# 1  
Old 10-17-2009
Calculate average of each of position of contents in a huge file

My input:
10 20 50 60 10 100 15 10
20 20 50 60 20 100 15 10
10 20 50 60 40 100 15 10
40 20 50 60 10 100 15 10
My Output
20 20 50 60 20 100 15 10

If I have a long list of file. I want to calculate average of each position inside the contents one by one.
Anybody got any idea about it?
Thanks for all of the suggestions.
# 2  
Old 10-17-2009
Put this in an file called avg.awk (name not important):

 a[1,8] = 0

        for(i=1; i<=8; i++)
                a[i] += $i

        for(i=1; i<=8; i++)
                b[i] = a[i]/count
        print b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8]

Then use this command line:

awk -f avg.awk input_file

where input_file is the path to the data file...
# 3  
Old 10-17-2009
awk '/^[0-9]/{for(i=0;++i<=NF;) a[i]=a[i]?(a[i]+$i):$i;c++}END{for(i=0;++i<=NF;) printf ("%s%s",(i==1)?"":FS,a[i]/c)}'  file

Will work for any number of fields.

Last edited by danmero; 10-17-2009 at 03:42 AM.. Reason: Fix typo
# 4  
Old 10-18-2009
Thanks a lot, danmero
Your code work well for my huge file.
It is fantastic Smilie

---------- Post updated at 11:09 PM ---------- Previous update was at 10:08 PM ----------

Hi danmero,
Can I ask you one more question about your code?
Is it your code can't work if the last line of the file is empty,right?
I just found out this problem when I try with a file that last line is empty.
Thanks again.
# 5  
Old 10-18-2009
Another awk solution:
awk '!(NR%2){a="";for(i=1;i<=NF;i++){t[i]+=$i; a=a FS t[i]/(NR/2)}}END{print substr(a,2)}' file

# 6  
Old 10-18-2009
Originally Posted by patrick87
Is it your code can't work if the last line of the file is empty,right?
Correct, for different number of fields or empty lines the following code should work
awk '/^[0-9]/{for(i=0;++i<=NF;) a[i]=a[i]?(a[i]+$i):$i;c++;max=(max>NF)?max:NF}END{for(i=0;++i<=max;) printf ("%s%s",(i==1)?"":FS,a[i]/c)}' file

# 7  
Old 10-18-2009
sed '/^>/d' a.txt | awk '{
        for(i in _)
        printf("%s ",_[i]/NR)

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 "kevin.c" POST ... (2 Replies)
Discussion started by: Newman
2 Replies

4. Shell Programming and Scripting

How to calculate average of two columns and copy into another file?

Hi, I need help with the awk command. I have a folder with aprox 500 files each one with two columns and I want to print in a new file, the average of column 1 and average of column 2 and the name of each file. Input files are: File-1: 100 99 20 99 50 99 50 99 File-2: 200 85... (3 Replies)
Discussion started by: Lokaps
3 Replies

5. UNIX Desktop Questions & Answers

Calculate average for rows in a text file

Dear Gurus, I have tab-delimited text files with matrix containing values. The first column is a identifier and other columns have the corresponding values. I would like to calculate the average value (total number/number of entries) for all entries from 2nd column to the last column in row... (3 Replies)
Discussion started by: Unilearn
3 Replies

6. Shell Programming and Scripting

Calculate average from CSV file using PERL script

Hi All I have this csv file and I need to calculate the average of FPS. FPS:27.7420, Interval:1314184238772 FPS:25.9798, Interval:1314184242646 FPS:27.4772, Interval:1314184246311 FPS:26.1623, Interval:1314184250159 FPS:26.4515, Interval:1314184253972 FPS:31.5896, Interval:1314184257163... (24 Replies)
Discussion started by: sayachop
24 Replies

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

8. Shell Programming and Scripting

Insert file contents into another file at a given position

I'm trying to write a small shell script/command to insert the contents of one file into another file at a position marked with a given text string. It's not necessarily at the top or bottom of the file so I can't just use cat to cat the files together. I think probably sed with the r option is... (5 Replies)
Discussion started by: shaun29
5 Replies

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

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