Difference between two files after average using s

2019-09-01 16:25发布

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

标签: linux shell awk
2条回答
看我几分像从前
2楼-- · 2019-09-01 16:54

Your awk will compute the averages but bash won't do floating point arithmetic. You can always use bc though.

$ echo "$ave1 - $ave2" | bc

-101.429

Also for expressions you have to use $(( ... ))

查看更多
够拽才男人
3楼-- · 2019-09-01 17:05
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
查看更多
登录 后发表回答