Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...