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*
This produces: