Difference between files

I have two files like follows: -

File 1: -

A 20
B 15
D 10
C 6

File 2: -

B 21
A 20
C 11
D 10

Now I want file like this: -
It should pick up 'A' from File 1 and pick up 'A' from File 2 and should get difference of the numbers in 2nd column and display result as follows

Result: -

  file1 file2 Diff

A 20 20 0
B 21 15 6
C 11 6 5
D 10 10 10

Something like this :

awk 'NR==FNR {a[$1]=$2;next} {if ($1 in a) { print $1 , a[$1] ,$2 , $2 - a[$1] } }' file1 file2 | sort

Hi Panyam,

I used the your code with a bit of modification to suit my requirement and it worked fine, Thanks

Code I used is: -

'FILENAME=="File1" {A[$1]=$2} FILENAME=="File2" {if (A[$1]) {print($1" "A[$1]" "$2" "A[$1]-$2)}}' File1 File2>>File3