使用shell脚本和awk平均入选作品后两个文件之间的差异 [英] Difference between two files after average of selected entries using shell script or awk

查看:103
本文介绍了使用shell脚本和awk平均入选作品后两个文件之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件。每个人都有一些丢失的数据一列9999,9000例如

  ifile1.txt ifile2.txt
30 20
9999 10
10 40
40 30
10月31日
29 9000
9000 9999
9999 9999
31 1250
550 29

我想计算值在上述两个文件的平均值(这是> 10)之间的差不考虑缺失值。即。

 平均(项>在ifile1.txt 10) - 平均(项大于10的ifile2.txt)

请注意:平均应采取在选定的数值仅供即那些> 10只例如

 (30 + 40 + 29 + 31 +五分之五百五十)在ifile1.txt

我问过类似的问题在这里<一个href=\"http://stackoverflow.com/questions/33540543/difference-between-two-files-after-average-using-shell-script-or-awk\">Difference使用shell脚本或AWK 并试图这样,但收到错误。

平均后两个文件之间

 的awk'($ 0→10)及和放大器; !/ 9000 | 9999 / {一个[ARGIND] + = $ 0; B〔ARGIND] ++} END {打印[1] / B [1] -a [2] / B [2]}的file1 file2的


解决方案

试试这个awk的:

 的awk'$ 1&GT; 10安培;&安培; $ 1〜/ ^(9000 | 9999)$ / {A [ARGIND] + = $ 1!; B〔ARGIND] ++}
       END {printf的%.2f \\ N,一个[1] / B [1] -a [2] / B [2]}'的ifile [12]的.txt

输出:

  -97.33

I have two files. Each has one column with some missing data as 9999, 9000. e.g.

ifile1.txt    ifile2.txt
30            20
9999          10
10            40
40            30
10            31
29            9000
9000          9999
9999          9999
31            1250
550           29

I would like to calculate the difference between the averages of the values (which are > 10) in the above two files without considering the missing values. i.e.

average ( the entries > 10 in ifile1.txt) - average (the entries > 10 in ifile2.txt)

Kindly note: The average should be taken over the selected values only i.e. those are > 10 only e.g.

(30+40+29+31+550/5) in ifile1.txt

I asked a similar question here Difference between two files after average using shell script or awk and tried like this, but getting error.

awk '($0>10) && !/9000|9999/{a[ARGIND]+=$0;b[ARGIND]++}END{print a[1]/b[1]-a[2]/b[2]}' file1 file2

解决方案

Try this awk:

awk '$1>10 && $1 !~ /^(9000|9999)$/{a[ARGIND]+=$1; b[ARGIND]++}
       END{printf "%.2f\n", a[1]/b[1]-a[2]/b[2]}' ifile[12].txt

Output:

-97.33

这篇关于使用shell脚本和awk平均入选作品后两个文件之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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