calculating average using awk from multiple files

前端 未结 4 1799
再見小時候
再見小時候 2020-12-10 15:57

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条回答
  •  -上瘾入骨i
    2020-12-10 16:19

    Here's a quick way using paste and awk:

    paste fort.* | awk '{ for(i=2;i<=NF;i+=2) array[$1]+=$i; if (i = NF) print $1, array[$1]/NF*2 }' > output.file
    

    Like some of the other answers; here's another way but this one uses sort to get numerically sorted output:

    awk '{ sum[$1]+=$2; cnt[$1]++ } END { for (i in sum) print i, sum[i]/cnt[i] | "sort -n" }' fort.*
    

提交回复
热议问题