Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculating average using awk from multiple files

Tags:

awk

I have 500 files with name fort.1, fort.2 ... fort.500. Each file contains 800 data as below:

1 0.485
2 0.028
3 0.100
4 0.979
5 0.338
6 0.891
7 0.415
8 0.368
9 0.245
10 0.489

I want to get the average of each line of second column from every file. In other words, get average of second column first line from all files and store in "output.file". Then get average of second column of second line and store in the same "output.file". I tried with paste command but fail to get what I want. IS there any way to do in AWK?

Appreciate any help. Thanks

like image 908
Vijay Avatar asked Nov 07 '12 05:11

Vijay


1 Answers

awk without any assumption on the 1st column:

awk '{a[FNR]+=$2;b[FNR]++;}END{for(i=1;i<=FNR;i++)print i,a[i]/b[i];}' fort.*
like image 87
Guru Avatar answered Sep 18 '22 06:09

Guru