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.
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 sideawk
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
.