Average of multiple files without considering missing values

前端 未结 3 1712
野的像风
野的像风 2021-01-24 05:09

I want to calculate the average of 15 files:- ifile1.txt, ifile2.txt, ....., ifile15.txt. Number of columns and rows of each file are same. But some of them are missing values.

3条回答
  •  时光取名叫无心
    2021-01-24 05:22

    awk '
       {
       for (i = 1;i <= NF;i++) {
          Sum[FNR,i]+=$i
          Count[FNR,i]+=$i!="?"
          }
       }
    END {
       for( i = 1; i <= FNR; i++){
          for( j = 1; j <= NF; j++) printf "%s ", Count[i,j] != 0 ? Sum[i,j]/Count[i,j] : "?"
          print ""
          }
       }
    ' ifile*
    

    assuming file are correctly feeded (no trailing empty space line, ...)

提交回复
热议问题