Average of multiple files without considering missing values

前端 未结 3 1696
野的像风
野的像风 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, ...)

    0 讨论(0)
  • 2021-01-24 05:25

    Use this:

    paste ifile*.txt | awk '{n=f=0; for(i=1;i<=NF;i++){if($i*1){f++;n+=$i}}; print n/f}'
    
    • paste will show all files side by side
    • awk calculates the averages per line:
      • n=f=0; set the variables to 0.
      • for(i=1;i<=NF;i++) loop trough all the fields.
      • if($i*1) if the field contains a digit (multiplication by 1 will succeed).
      • f++;n+=$i increment f (number of fields with digits) and sum up n.
      • print n/f calculate n/f.
    0 讨论(0)
  • 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 '?' ...

    0 讨论(0)
提交回复
热议问题