Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to diff two files and ignore lines that are present in one file but missing in the other.

For example

File1:

foo
bar
baz
bat

File2:

foo
ball
bat

I'm currently running the following diff command

diff File1 File2 --changed-group-format='%>' --unchanged-group-format=''

Which in this case would produce

bar
baz

as the output, i.e. only missing or conflicting lines. I would like to only print conflicting lines, i.e. ignore cases where one line is missing from File2 and is present in File1 (not the other way around). Is there any way to do something like this using diff or do I have to resort to other tools? If so, what would you recommend?

share|improve this question
1  
If your input files could be sorted, the comm command might do what you want (assuming it is available under cygwin) –  Brian Swift Apr 6 '12 at 23:17

2 Answers 2

up vote 2 down vote accepted

You might also take a look at comm, if you have it available:

comm [-1] [-2] [-3 ] file1 file2
-1 Suppress the output column of lines unique to file1.
-2 Suppress the output column of lines unique to file2.
-3 Suppress the output column of lines duplicated in file1 and file2.

The input files should be sorted. However, you can modify the default behavior with --nocheck-order option, if available.

In your case you would want comm --nocheck-order -23 file filter_file

share|improve this answer

When you say conflicting, do you mean entries that appear in both File1 and FIle2?

If so, use the following -

Create a shell script called mycmopare.sh and put the below into it.

#!/bin/bash
File1Contents=$(cat File1)

for i in $File1Contents; do
   grep $i File2
done

Run mycompare.sh from the directory where File1 and File2 are stored.

Output:

foo
bat
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.