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

查看:308
本文介绍了平均使用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

我想计算上述两个文件的平均值之间的差而不考虑缺失值。即。

 平均(ifile1.txt) - 平均(ifile2.txt)

我试过这样,但没有得到结果。

  AVE1 = $(awk的'/ \\ 9999 /&安培;!&安培;!/ \\ 9000 / {总和+ = $ 1;计数++} END {打印计数(总和/计数) :计数;总和=计数= 0}ifile1.txt)
AVE2 = $(awk的'/ \\ 9999 /&放大器;&安培; / \\ 9000 / {总和+ = $ 1;计数++} END!{打印计数(总和/计数):计数;总和=计数= 0}ifile2。文本)结果= $(AVE1-AVE2)
回声$结果


解决方案

 的awk'/ 9000 | 9999 / {A [名] + = $ 0; B〔文件名] ++}! END {为(我的)C = C CA [I] / b [I]:A [I] / b [I];打印c}里'文件1文件2

更新:

 的awk'/ 9000 | 9999 / {A [ARGIND] + = $ 0; B〔ARGIND] ++}!END {打印[1] / B [1] -a [2] / b [2]}的file1 file2的

 的awk'/ 9000 | 9999 / {A [ARGIND] + = $ 0; B〔ARGIND] ++}!END {为(i = 1; I< = ARGIND;我++ )C = C CA [I] / b [I]:A [I] / b [I];打印c}里'文件1文件2

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 above two files without considering the missing values. i.e.

average (ifile1.txt) - average (ifile2.txt)

I tried like this, but not getting the result.

ave1=$(awk '!/\9999/ && !/\9000/{sum += $1; count++} END {print count ? (sum/count) : count;sum=count=0}' ifile1.txt)
ave2=$(awk '!/\9999/ && !/\9000/{sum += $1; count++} END {print count ? (sum/count) : count;sum=count=0}' ifile2.txt)

result=$(ave1-ave2)
echo $result

解决方案

awk '!/9000|9999/{a[FILENAME]+=$0;b[FILENAME]++}END{for(i in a)c=c?c-a[i]/b[i]:a[i]/b[i];print c}' file1 file2

Update:

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

or

awk '!/9000|9999/{a[ARGIND]+=$0;b[ARGIND]++}END{for(i=1;i<=ARGIND;i++)c=c?c-a[i]/b[i]:a[i]/b[i];print c}' file1 file2

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

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