Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to output differences between two files (preferably using command line)

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.

like image 895
the_new_mr Avatar asked Dec 16 '11 12:12

the_new_mr


People also ask

Which command shows the differences between two files line by line?

Use the diff command to compare text files. It can compare single files or the contents of directories. When the diff command is run on regular files, and when it compares text files in different directories, the diff command tells which lines must be changed in the files so that they match.

How can I compare two files for differences?

Right-click on the first file. Click on “Select for Compare” from the menu. Proceed to right-click on the second file. Click on “Compare with Selected.

How do I find the difference between two text files in terminal?

The diff command compares two files and produces a list of the differences between the two. To be more accurate, it produces a list of the changes that would need to be made to the first file, to make it match the second file. If you keep that in mind you'll find it easier to understand the output from diff .

Which command can give first found difference between two files?

diff stands for difference. This command is used to display the differences in the files by comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in one file have is to be changed to make the two files identical.


2 Answers

This might work for you (GNU diff):

diff -u file1 file2 | sed -n '1,2d;/^[-+]/p' +ball -flan +elephant 
like image 140
potong Avatar answered Sep 29 '22 11:09

potong


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 
like image 43
The Archetypal Paul Avatar answered Sep 29 '22 12:09

The Archetypal Paul