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

**URL:** <https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [January 23, 2012, 2:33pm UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245 "2012-01-23T14:33:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![evelibertine](https://community.unix.com/letter_avatar/evelibertine/32/5_5575768a8748004e209b776fc1b2916d.png) [@evelibertine](https://community.unix.com/u/evelibertine)\
**Post date:** [January 23, 2012, 2:33pm UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245/1 "2012-01-23T14:33:35Z")

</div>

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:

```nohighlight
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:

```nohighlight
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:

```nohighlight
150 rs1106175 0.00 0.8000

```

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [January 23, 2012, 3:34pm UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245/2 "2012-01-23T15:34:26Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![royalibrahim](https://community.unix.com/letter_avatar/royalibrahim/32/5_5575768a8748004e209b776fc1b2916d.png) [@royalibrahim](https://community.unix.com/u/royalibrahim)\
**Post date:** [January 24, 2012, 6:42am UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245/3 "2012-01-24T06:42:32Z")

</div>

> [@radoulov](#):
>
> ```plaintext
> awk > file3 'NR == FNR {
> f1[$1, $2, $3] = $4
> next
> }
> ($1, $2, $3) in f1 && f1[$1, $2, $3] != $4
> ' file1 file2  
> 
> ```

@[radoulov](http://www.unix.com/members/302021308.html), the above code is not doing the right thing. It produces empty result. Could you please check once?

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [January 24, 2012, 6:44am UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245/4 "2012-01-24T06:44:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![royalibrahim](https://community.unix.com/letter_avatar/royalibrahim/32/5_5575768a8748004e209b776fc1b2916d.png) [@royalibrahim](https://community.unix.com/u/royalibrahim)\
**Post date:** [January 24, 2012, 6:49am UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245/5 "2012-01-24T06:49:39Z")

</div>

> [@radoulov](#):
>
> 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

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

```

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [January 24, 2012, 6:51am UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245/6 "2012-01-24T06:51:46Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![royalibrahim](https://community.unix.com/letter_avatar/royalibrahim/32/5_5575768a8748004e209b776fc1b2916d.png) [@royalibrahim](https://community.unix.com/u/royalibrahim)\
**Post date:** [January 24, 2012, 6:55am UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245/7 "2012-01-24T06:55:28Z")

</div>

> [@radoulov](#):
>
> 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 ☹

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [January 24, 2012, 6:59am UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245/8 "2012-01-24T06:59:54Z")

</div>

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

```nohighlight
% 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.

---

<div class="post-metadata">

**Author:** ![parthmittal2007](https://community.unix.com/letter_avatar/parthmittal2007/32/5_5575768a8748004e209b776fc1b2916d.png) [@parthmittal2007](https://community.unix.com/u/parthmittal2007)\
**Post date:** [January 24, 2012, 9:22am UTC](https://community.unix.com/t/comparing-two-text-files-by-a-column-and-printing-values-that-do-not-match/303245/9 "2012-01-24T09:22:54Z")

</div>

Use this

```nohighlight
comm -13 f1 f2

```

f1-file1  
f2-file2
