How to take a Average of numbers from different files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to take a Average of numbers from different files?
# 1  
Old 06-17-2013
How to take a Average of numbers from different files?

Hi,

I have 3 to 4 different files, from that i need to take a Average of numbers from a particular column. here i have to take 4th column,
that should present in diff. file.

File 1:
Code:
Col1 col2 col3 col4
1 11 sa 12.00
2 22 sb 134.59
3 33 sc 11.99
4 44 sd 12.44

Code:
Col1 col2 col3 col4
1 11 sa 11.00
2 22 sb 114.99
3 33 sc 14.99
4 44 sd 19.44

Code:
Col1 col2 col3 col4
1 11 sa 13.00
2 22 sb 124.99
3 33 sc 15.99
4 44 sd 17.44

Please help me to do this.
# 2  
Old 06-17-2013
Code:
awk '/^[0-9]/{a+=$4;c++}END{print "Avg: " a/c}' file*

# 3  
Old 06-17-2013
Code:
perl -ne 'if (/\w+\s\w+\s\w+\s(\d+\.\d+)){$total+=$1;$count++} END{printf "%0.2f",$total/$count;}' file1 file2 file3

Modify the regex to suit your expected data types.
# 4  
Old 06-17-2013
You mean you want to calculate average of 4th column in every file?
You can use below on every file...

awk 'NR>1{a+=$4;b++}END{print "Average: " a/b}' file
# 5  
Old 06-17-2013
Thanks you every one,

but i forget to mention that i have match the 2nd col. value in every file if it matches then i need to take the average of those 4th col. value only not in column wise

Ex: 11 is match i need to take average 12.00, 11.00 and 13.00
# 6  
Old 06-17-2013
Quote:
Originally Posted by Yoda
Code:
awk '/^[0-9]/{a+=$4;c++}END{print "Avg: " a/c}' file*

Yoda: Why I get different answers when I use your script and my script below:

awk 'NR>1{a+=$4;b++}END{print "Average: " a/b}' file[123]

I get 41.905 with your script and 35.9186 using my script, which I guess is not correct. I don't see any difference except NR>1
Does the contents of all the three files gets concatenated before it is feed to awk? I guess NR>1 will remove header from first file, but will not remove headers from other two files. Is it so?
# 7  
Old 06-17-2013
You should use FNR instead of NR:
Code:
awk 'FNR>1{...

Check the man page to understand the difference between NR and FNR

You could also run below code to see the difference:
Code:
awk '{ print NR, FNR, FILENAME }' file[123]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Average of a columns from three files

hello, I have three files in the following order ==> File1 <== 1 20977000 20977000 A C 1.00 0,15 15 45 1 115829313 115829313 G A 0.500 6,7 13 99 ==> File2 <== 1 20977000 20977000 A C 1.00 0,13 13 39 1 115829313 ... (5 Replies)
Discussion started by: nans
5 Replies

2. Shell Programming and Scripting

Calculating average from files

I have some files with the following contents.I would like to calculate average of fifth column. How can I do this with awk? file1 cat 95.9 152 78.0 17.9 rat 67.1 153 36.5 30.6 dog 81.4 154 68.1 13.3 dog 92.0 155 55.5 36.5 rat 73.8 156 23.9 49.9 file2 rat... (4 Replies)
Discussion started by: avina
4 Replies

3. Shell Programming and Scripting

Match first two columns and average third from multiple files

I have the following format of input from multiple files File 1 24.01 -81.01 1.0 24.02 -81.02 5.0 24.03 -81.03 0.0 File 2 24.01 -81.01 2.0 24.02 -81.02 -5.0 24.03 -81.03 10.0 I need to scan through the files and when the first 2 columns match I... (18 Replies)
Discussion started by: ncwxpanther
18 Replies

4. Solaris

How is Load Average computed and what are better numbers?

Hello All, What is load average and how is it computed in Solaris 10? What are the different ranges for normal, warning and danger signs? Kindly clarify. Thank you, Sunil Kumar (3 Replies)
Discussion started by: msgforsunil
3 Replies

5. Shell Programming and Scripting

Number of elements, average value, min & max from a list of numbers using awk

Hi all, I have a list of numbers. I need an awk command to find out the numbers of elements (number of numbers, sort to speak), the average value the min and max value. Reading the list only once, with awk. Any ideas? Thanks! (5 Replies)
Discussion started by: black_fender
5 Replies

6. Shell Programming and Scripting

Average of a column in multiple files

I have several sequential files with name stat.1000, stat.1001....to stat.1020 with a format like this 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0... (6 Replies)
Discussion started by: kayak
6 Replies

7. Shell Programming and Scripting

Average same line over different files

Hi Reason tells me that answer to this question should be somewhere on the internet, But I ve not been able to find it. Here is what I wanna do, for multiple files like this file1: 2 x x x 200 3 x x 400 5 x x 600 file2: 3 x x 500 6 x x 600 2 x x 500 I want to get an output file... (6 Replies)
Discussion started by: jamie_123
6 Replies

8. UNIX for Dummies Questions & Answers

Taking a average of a column of numbers

Hey all, I am relatively poor at programming and unfortunately don't have time to read about programming at this current moment. I wanted to be able to run a simple command to read a column of numbers in a file and give me the average of those numbers. In addition if I could specify the... (2 Replies)
Discussion started by: Leonidsg
2 Replies

9. Shell Programming and Scripting

Extract numbers from text file work out average

Just wondering if someone could assist me with shell script I'm trying to write. I need to read the final column of a text file (shown below) and workout what the average number is. The text file will have a variable number of lines, I just want the script to pull out the values in the final field... (14 Replies)
Discussion started by: rich@ardz
14 Replies

10. Shell Programming and Scripting

BASH. Need to extract some numbers and take the average

Hey all, I ran some simulations, of which the output is 100s of files. I've used grep to extract the vital information needed from the files. This has made my task somewhat easier. But I still need to perform some mathematical calculations (average and geometrical average) on the results of the... (9 Replies)
Discussion started by: slackjack
9 Replies
Login or Register to Ask a Question