Average of multiple files without considering missing values

前端 未结 3 1697
野的像风
野的像风 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:31

    awk 'FNR == 1 { nfiles++; ncols = NF }
      { for (i = 1; i < NF; i++) 
            if ( $i != "?" ) { sum[FNR,i] += $i ; count[FNR,i]++ ;}
       if (FNR > maxnr) maxnr = FNR
      }
      END {
      for (line = 1; line <= maxnr; line++)
      {
         for (col = 1; col < ncols; col++)
              if ( count[line,col] > 0 ) printf "  %f", sum[line,col]/count[line,col];
              else printf " ? " ;
         printf "\n" ;
      }
    }' ifile*.txt
    

    I just check the '?' ...

提交回复
热议问题