Linux 如果满足shell脚本中的给定条件,则计算平均值

Linux 如果满足shell脚本中的给定条件,则计算平均值,linux,shell,awk,Linux,Shell,Awk,我有一个数据集,其中包含许多类型的缺失值,例如9990、9999、9999000、9999999等等。但都大于9990。我想取24个值的平均值。我正在尝试以下命令,但没有得到我的愿望输出 awk '{if ($1 < 9990) sum += $1; count++} NR%24==0{print count ? (sum) :9999;sum=count=0}'ifile 我试过这个,但结果不同: awk '{if ($1 < 9990)sum += $1; count++} N

我有一个数据集,其中包含许多类型的缺失值,例如9990、9999、9999000、9999999等等。但都大于9990。我想取24个值的平均值。我正在尝试以下命令,但没有得到我的愿望输出

awk '{if ($1 < 9990) sum += $1; count++} NR%24==0{print count ? (sum) :9999;sum=count=0}'ifile
我试过这个,但结果不同:

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

然后,
if
语句以下一个分号结束,而不是结束括号。

为什么在所有其他情况下,大于9990的值被忽略时,9999、9991和99954的平均值等于9999?您的awk脚本只对大于9990的行求和。这不正是你想要的吗?您是否需要24或3个值的平均值?对于24,您的示例数据中没有足够的数据。是的。我现在改了。但仍然没有打印期望的结果。我想我们都在指出,这个问题、您的尝试和期望的输出似乎都解决了不同的问题-到底是哪个?例如,当一组三行中的所有三行的值都大于9990,即总和等于零时-为什么“平均”9999?那是从哪里来的?
awk '{if ($1 < 9990)sum += $1; count++} NR%3==0{print count ? (sum/count) :9999;sum=count=0}'ifile
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.
$1 < 9990 {
    sum += $1;
    count++;
}
NR % 3 == 0 {
    if (count == 0) {
        print "9999";
    } else {
        print sum / count;
    }
    sum = 0;
    count = 0;
}
 {if ($1 < 9990) sum += $1; count++}