Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

awk - calculating average of same lines of second column from many files

I need a help on doing some operation on multiple files using awk. I have total of 500 files which each file contains 800 lines of data with two columns (1st column and 2nd column).

I want the first line, 2nd column of each file (from all 500 files) to be added and calculate the average and store in a new output file (lets say average.out).

Then it goes on to second line, 2nd column of all files (all 500 files) and calculate the average and store in average.out. And it continues until the "average.out" file contain 800 lines.

I badly need to solve this calculation before going on with other calculations of my data. Hope I get some insight.

thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This will display all the line numbers and their averages.

awk '{a[FNR]=a[FNR]+$2;b[FNR]++;}END{for(i in a){print i,a[i]/b[i]}}' *your_file*

for eg:

line_number its_average.

testted below:

> cat temp
1 10 
2 28
3 20
4 79
> cat temp2
1 12 
2 30
3 22
4 81
> awk '{a[FNR]=a[FNR]+$2;b[FNR]++;}END{for(i in a){print i,a[i]/b[i]}}' temp temp2
2 29
3 21
4 80
1 11

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...