Kayan Kayan - 6 months ago 7
Bash Question

Compute average if satisfies the given condition in shell script

I have a dataset with many kind of missing values such as 9990, 9999, 9999000, 999999 and many more. But all are greater than 9990. I would like to take average of each 24 values. I am trying with the following command but not getting my desire output.

awk '{if ($1 < 9990) sum += $1; count++} NR%24==0{print count ? (sum) :9999;sum=count=0}'ifile


For example: I need average of each 3 lines in the following data

3
3
4
9999
4
99990
13
3
999999
9999
9991
99954


I tried with this, but showing different result:

awk '{if ($1 < 9990)sum += $1; count++} NR%3==0{print count ? (sum/count) :9999;sum=count=0}'ifile


My desire output is

3.33
4 Average of 9999 4 99990 is done with 4/1. Because 9999 and 99990 are undefined values.
8 Average of 13 3 999999 is done with (13+8)/2. Because 999999 is an undefined value, so excluded from the average.
9999 All are undefined values, so denoted as 9999.

Answer
$1 < 9990 {
    sum += $1;
    count++;
}
NR % 3 == 0 {
    if (count == 0) {
        print "9999";
    } else {
        print sum / count;
    }
    sum = 0;
    count = 0;
}

Your mistake is to increment count when the value is "undefined". If you write

 {if ($1 < 9990) sum += $1; count++} 

then the if statement ends at the next semicolon, not at the closing bracket.