Find file size difference in two files using awk

Hi,

Could anyone help me to solve this problem?

I have two files "f1" and "f2" having 2 fields in each, a) file size and b) file name. The data are almost same in both the files except for few and new additional lines. Now, I have to find out and print the output as, the difference in the "file size" for the file names present in the f1 and f2 and also, fields that are not present in both the files.

Note: the number of lines in the files may differ. And the file sizes are in KB.

For eg: suppose if the files are

  
   f1                   f2
-----------       -----------
2   a.xml          9    a.xml
7   b.xml          7    b.xml
3   c.xml          3    c.xml
6   d.xml          9    d.xml
4   t1.def         8    t3.def
5   f3.jpg

Output should be:

7    a.xml 
3    d.xml
4    t1.def
8    t3.def
5    f3.jpg

Try:

awk 'NR==FNR{a[$2]=$1}NR!=FNR{a[$2]-=$1}END{for (i in a){if (a!=0){print (a<0)?-a:a,i}}}' f1 f2

Thank you bartus, it worked perfectly. But, I cannot decode the logic. Could you please explain this to me?

Few doubts in this:
NR!=FNR --> this is needed because I told that counting of the lines may not be same in both the files? Or this is an indicator to parse the second file? If that so, this explicit indication is really needed to make awk to parse the 2nd file? Please clarify me.

Suppose if I want to print the common lines (similar lines not the unique) in both the files also, then I have to just remove the condition if (a[i]!=0)?

You second guess is correct. NR==FNR means parsing 1st file, and NR!=FNR - second. It can be written differently thought:

'NR==FNR{a[$2]=$1;next}{a[$2]...