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:

Col1 col2 col3 col4
1 11 sa 12.00
2 22 sb 134.59
3 33 sc 11.99
4 44 sd 12.44
Col1 col2 col3 col4
1 11 sa 11.00
2 22 sb 114.99
3 33 sc 14.99
4 44 sd 19.44
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.

awk '/^[0-9]/{a+=$4;c++}END{print "Avg: " a/c}' file*
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.

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

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

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?

You should use FNR instead of NR :

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:

awk '{ print NR, FNR, FILENAME }' file[123]

Try

awk     '$COL==MATCH    {SUM+=$4;CNT++}
         END            {print "Average: " SUM/CNT}
        ' COL=2 MATCH=11 file{1..3}
Average: 12

please check the new post for more details: http://www.unix.com/shell-programming-scripting/228569-find-average-different.html\#post302823181