29

I am familiar with tools such as tkDiff and WinMerge and am aware of how to see the difference between two files.

What I'm looking to do is to produce a report of elements in one file that are not present in another.

For example:

File1 contains:

apple
cool
dude
flan

File2 contains:

apple
ball
cool
dude
elephant

I want to produce a report that contains:

ball
elephant

Or, better yet, a report like this:

+ball
+elephant
-flan

Does anybody know of a tool that can do this? Preferably with command line options.

The report feature in WinMerge isn't too far off what I'd like but there is no command line option to do this (as far as I know).

Thanks in advance.

3 Answers 3

47

This might work for you (GNU diff):

diff -u file1 file2 | sed -n '1,2d;/^[-+]/p'
+ball
-flan
+elephant
1
  • Excellent. A better answer, since works on unsorted files. The 'sed' addition of +/- is a nice touch (required by the original question). Apr 10, 2013 at 23:47
26

You probably want the Unix comm utility. Windows versions are included in gnuwin32

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 lines unique to FILE1

   -2     suppress lines unique to FILE2

   -3     suppress lines that appear in both files
8
  • Very nice. Thanks for this. The whole GnuWin32 is something I wasn't aware of and is clearly complete GOLD! By using a combination of -1 and -3, I was able to get what I wanted. Shame there's no + and - though but I guess I shouldn't be too greedy.
    – the_new_mr
    Dec 16, 2011 at 12:37
  • 1
    A bit of work with sed (also in the utils) should let you add the +/- Dec 16, 2011 at 14:45
  • 1
    NICE! I had to do a diff between two datasets, and comparing 1.1 million rows vs 8 million rows was going to take too long. I dumped to .CSV, and used cygwin: comm -2 -3 file1.csv file2.csv > out.csv It ran in 13 seconds! Sep 19, 2013 at 18:52
  • Nice answer. but how to save output as a file? Feb 21, 2017 at 19:07
  • 2
    Redirect it, same as any other CLI command. comm -1 file1 file2 > results, for example Feb 21, 2017 at 20:07
-4

To compare data with command prompt you can use

COMP /a /l D:\Folder1\data.txt D:\Folder2\data.txt

Here are other options to compare files and folders with command prompt.

2
  • 1
    I dont see how this answer is relevant to the question.
    – manWe
    Jan 7, 2014 at 7:51
  • I didn't get anything useful from it: "Files are different sizes.". The result I get from FC is more useful.
    – ofthelit
    Jan 21, 2016 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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