Average of multiple files without considering diff

2019-07-08 19:05发布

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 having different type of missing values (for instance ?, -9999 & 8888). Part of the data looks as

 ifile1.txt             ifile2.txt               ifile3.txt
 2  8888   ?     ? .    1  2     1     3    .    5  ?  ?  ? .
 1  -9999  8888  ? .    1  8888  8888  8888 .    5  ?  ?  ? .
 4  6      5     2 .    2  5     5     1    .    3  4  3  1 .
 5  5      7     1 .    0  0     1     1    .    4  3  4  0 .
 .  .      .     . .    .  .     .     .    .    .  .  .  . .  

I would like to find a new file which will show the average of these 15 fils without considering the missing values.

 ofile.txt
 2.66     2        1         3      . (i.e. average of 2 1 5, average of 8888 2 ? and so on)
 2.33     -9999    -9999    -9999   .
 3        5        4.33      1.33   .
 3        2.66     4         0.66   .
 .      .     .    .      .

This question is similar to my earlier question Average of multiple files without considering missing values

I was trying with the following, but not getting the desired result.

awk '
   {
     for (i = 1;i <= NF;i++) {
     Sum[FNR,i]+=$i
     Count[FNR,i]+=$i!="?|-9999|8888"
   }
     }
  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] : "?|-9999|8888"
       print "-9999"
      }
     }
   ' ifile*

标签: shell awk
1条回答
爷的心禁止访问
2楼-- · 2019-07-08 19:56
awk '
{
    for (i=1; i<=NF; i++) {
        if ($i !~ /^([?]|-9999|8888)$/) {
            Count[FNR,i]++
            Sum[FNR,i]+=$i
        }
  }
}
END {
   for (i=1; i<=FNR;i++){
       for (j=1; j<=NF; j++)
           printf "%12.2f ", Count[i,j]!=0 ? Sum[i,j]/Count[i,j] : -9999
       print ""
   }
}
' ifile*.txt

This produces:

    2.67         2.00         1.00         3.00 
    2.33     -9999.00     -9999.00     -9999.00 
    3.00         5.00         4.33         1.33 
    3.00         2.67         4.00         0.67 
查看更多
登录 后发表回答