compare files and calculating

Hi everybody:
Could anybody tell me how can I do this task in AWK language.
I've two files (file1 & file2) where file1 its last row it has first field value equal that the first row in file2, thyen I want calculate the difference in other fields, and this values apply in file2. This is:

file1:
.............................
.............................
10.87 228.983
14.31 211.306
19.83 214.468

file2 :
19.83 217.83
28.67 231.34
42.74 264.576
.............................
.............................

Then diff=$2(file2)-$2(file1)=217.83-214.468=3.362, then I would like to have this output:
19.83 214.468
28.67 231.34-diff
42.74 264.576-diff
.............................
.............................

This is:
19.83 214.468
28.67 227.978
42.74 261.214
.............................
.............................

Thanls in advance and cheers. :slight_smile:

tail -1 file1 | read x y
head -1 file2 | read a b
if [ $a -eq $x ]
then
    diff=$( echo $b - $y | bc )
    awk -v dif=$diff ' { print $1 , ( $2 - dif ) } ' file2
fi

Thanks Ambu23, but Ive tried to use it and I don know why, it appears this error:
./gir_int.sh: line 72: bc: command not found
I hope that it is because this command are not installed (?).
What do you recommend me ?. :slight_smile:

tail -1 file1 | read x y
head -1 file2 | read a b
if [ $a -eq $x ]
then
    awk -v var1=$b -v var2=$y ' BEGIN { dif=var1 - var2 } { print $1 , ( $2 - dif ) } ' file2
fi

Thanks again.
Now I've got an ouput file like:

19.83 217.83
28.67 231.34
42.74 264.576
65 232.5
100 210

but the second column does not correspond with the difference.
I did this (in my case):

tail -1 05080300.ddr | read x y z
head -1 mls_65.tmp1 | read a b c
if [ $a -eq $x ]
then
LC_ALL=en_DK awk -v var1=$b -v var2=$y ' BEGIN { dif=var1-var2 } { print $1, ($2-dif) } ' mls_65.tmp1 > mls_65.tmp2 #file2
fi

Really my files it has got three columns. :slight_smile:

Try...

$ awk 'FNR==NR{x=$2;next}FNR==1{d=$2-x;$2=x}FNR>1{$2-=d}1' file1 file2
19.83 214.468
28.67 227.978
42.74 261.214

Thanks a lot Ygor. It works correctly. Cheers :smiley: