Comparing two text files by a column and printing values that do not match

I have two text files where the first three columns are exactly the same. I want to compare the fourth column of the text files and if the values are different, print that row into a new output file. How do I go about doing that?

File 1:

100 rs3794811    0.01 0.3434
100 rs8066551    0.01 0.4541
150 rs1106175    0.00 0.5562
150 rs7224313    0.03 0.1484

File 2:

100 rs3794811    0.01 0.3434
100 rs8066551    0.01 0.4541
150 rs1106175    0.00 0.8000
150 rs7224313    0.03 0.1484

Output:

150 rs1106175    0.00 0.8000
awk > file3 'NR == FNR {
  f1[$1, $2, $3] = $4
  next
  }
($1, $2, $3) in f1 && f1[$1, $2, $3] != $4
  ' file1 file2  

@radoulov, the above code is not doing the right thing. It produces empty result. Could you please check once?

What's your awk version and operating system?
Do you get empty result with the exact samples that you posted?

I am using Ubuntu 8.04, having GNU Awk 3.1.6.

Yes, the code you mentioned above returning empty results to me with the exact samples posted by the user.

I tweaked the code a little to get some results as shown below but still not able to achieve the expected result

awk > file3 'NR==FNR {f=$1 FS $2 FS $3; a[f]=$4; next} a[f] != $4' file1 file2

Hmm, sorry if I'm asking for the obvious, but did you check the content of file3 (the OP wants the output in a separate file).

Yes, I ran it both the ways, with file3 and without redirecting to file3 (i.e) to stdout :frowning:

Try to run the exact code I posted (without any modification!):

% head file[12]
==> file1 <==
100 rs3794811    0.01 0.3434
100 rs8066551    0.01 0.4541
150 rs1106175    0.00 0.5562
150 rs7224313    0.03 0.1484

==> file2 <==
100 rs3794811    0.01 0.3434
100 rs8066551    0.01 0.4541
150 rs1106175    0.00 0.8000
150 rs7224313    0.03 0.1484
% awk  'NR == FNR {
  f1[$1, $2, $3] = $4
  next
  }
($1, $2, $3) in f1 && f1[$1, $2, $3] != $4
  ' file1 file2
150 rs1106175    0.00 0.8000

Run the commands above and copy/paste the output (commands included) from your terminal.

Use this

comm -13 f1 f2

f1-file1
f2-file2