Compare 2 files using third file as an exception

Hi All,

Is it possible to compare 2 files using the contents of a third file as a guide using a bash script?

I have 2 files I want t compare (1 and 2 below) the difference between the two is the first line (a)

I want my script to ignore this difference if the difference is stored in file 3.

Is it possible to compare two files with the contents of the 3rd acting as an exception?

Thanks

The comm utility should be useful for this:

$ man comm
COMM(1)                          User Commands                         COMM(1)



NAME
       comm - compare two sorted files line by line

SYNOPSIS
       comm [OPTION]... FILE1 FILE2

DESCRIPTION
       Compare sorted files FILE1 and FILE2 line by line.

       With  no  options,  produce  three-column  output.  Column one contains
       lines unique to FILE1, column two contains lines unique to  FILE2,  and
       column three contains lines common to both files.

       -1     suppress column 1 (lines unique to FILE1)

       -2     suppress column 2 (lines unique to FILE2)

       -3     suppress column 3 (lines that appear in both files)
...

And you can use awk to check for exceptions.

So, comm -2 -3 file1 file2 | awk 'NR==FNR { A[$0]=1 ; next } !($0 in A)' file3 -

Hi Corona whether its file3 - or - file3 ? I am bit confused.

OR

$ awk 'FNR==1{++i}i==1{A[$0];next}i==2 && ($0 in A){delete A[$0];next}!($0 in A)' file1 file2 file3

Hi Guys,

Thanks for your quick responses.

Both of the above work as the files are now.

However if I make a change to file2 like remove b. I am returned with a blank line.

I should have mentioned above that if there is a difference and it is not noted in file3 I would like it to be returned if possible.

Thanks

You want it to read the list of rejects first -- only then can it tell which things from standard input to ignore. So it is file3 - .

Thanks Corona... actually because of this I confused, trying to reset mind :slight_smile:

$ echo "Pipe -- 1st" | awk '1' file3 -
a 
Pipe -- 1st

$ echo "Pipe -- 1st" | awk 'FNR==NR{print $0,"stdin";next}{print $0,FILENAME}' - file3
Pipe -- 1st stdin
a  3

$ echo "Pipe -- 1st" | awk 'FNR==NR{print $0,"stdin";next}{print $0,FILENAME}' file3 -
a  stdin
Pipe -- 1st -

---------- Post updated Feb 14th, 2014 at 12:14 AM ---------- Previous update was Feb 13th, 2014 at 11:47 PM ----------

@ nwalsh88 : show your real input as well as desired output (use codetags).