Kay
Kay

Reputation: 2067

Difference between two files after average using shell script or awk

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

Upvotes: 0

Views: 88

Answers (2)

bian
bian

Reputation: 1456

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

Upvotes: 2

karakfa
karakfa

Reputation: 67497

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

Upvotes: 1

Related Questions