Compare two tab-delimiter files

Hi,

I have two files like:

file1

chr1     40
chr1     50
chr2     10
chr2     60

file2

chr1     30
chr1     50
chr2     15
chr2     20

and want to get the difference of column 2 when column 1 is the same in both files.

So the output file would be:

chr1     10
chr1     0
chr2     -5
chr2     40

Could somebody help me? Thanks

with the recent fury of questions you posted... What have you tried so far and where exactly are you stuck?

I wanted to get the average of two columns and compare that average to a column in another file. I have the average in column 2 in in column 1 the chromosome number. If column 1 is the same for both files i want to calculate the difference between column 2 of two files as described above.
Sorry for all questions. im really new with this.

This is a very confusing explanation and seems that your original statement has nothing to do with the what're asking now and with the sample file you provided.
If the last statement is what you're after, please provide sample files and the desired output.
Also please show your own attempt at the resolution - it would help us guide you in the right direction.

Based on your sample datas, you can start with:

~/unix.com$ awk -F"\t" 'NR==FNR{A[NR]=$2;next}{print $1,$2-A[FNR]}' file1 file2