平均计算,如果满足在shell脚本的给定条件 [英] Compute average if satisfies the given condition in shell script

查看:118
本文介绍了平均计算,如果满足在shell脚本的给定条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多种缺失值,如9990,9999,9999000,999999和更多的数据集。但是,所有比9990更大,我想每次取24值的平均值。我想用下面的命令,但没有得到我的愿望输出。

 的awk'{如果($ 1 LT; 9990)之和+ = $ 1;算上++} NR%24 == 0 {打印算什么? (总和):9999;总和=计数= 0}IFILE

例如:我需要每3条线的平均水平。在下面的数据

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

我试过这个,但显示不同的结果:

 的awk'{如果($ 1 LT; 9990)之和+ = $ 1;算上++} NR%3 == 0 {打印计数? (总和/计数):9999;总和=计数= 0}IFILE

我的愿望输出

  3.33
9999 4 99990 4平均用4/1完成。由于9999和99990是不确定的值。
13 3 999999 8平均用(13 + 8)/ 2完成。因为999999是未定义的值,所以排除在平均。
9999全部是不确定的值,因此表示为9999。


解决方案

  $ 1< 9990 {
    总和+ = $ 1;
    算上++;
}
NR%3 == 0 {
    如果(计数== 0){
        打印9999;
    }其他{
        打印总和/计数;
    }
    总和= 0;
    计数= 0;
}

您的错误是递增计数当值为undefined。如果你写

  {如果($ 1 LT; 9990)之和+ = $ 1;算上++}

那么如果语句在下一个分号结束,而不是在右括号。

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.

解决方案

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

这篇关于平均计算,如果满足在shell脚本的给定条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆