Pages

Comparing 3 files

diff and diff3 : Both are commands for comparing  files and showing the differences.

The diff command is used to compare two files and list out the lines that are different between the files.
For Eg:
If we have tow files "one" and "two" having contents as follows

"one" :

This 
is the
first 
file 


"two" :

This 
is the
second 
file 

$ diff one two 
3c3
< first
---
> second


The 3c3 signifies the line numbers in which the files differ. The set of numbers before c is for the first file, and the numbers after c specifies the line numbers in the second file. 

The lines printed out are the lines that do not match. In the output, the line that begins with "<"  belongs to the first file and the line that starts with ">" belongs to the second file and these are separated by "---".

In case the number of files that you want to compare are 3 then diff would not be able to do it.
To compare 3 files we can use diff3 command.
Let us say we have another file named "three"

three: 
This 
is the 
third 
file. 


$diff3 one two three 
1:3c
  first
2:3c
  second
3:3c
  third

The output looks a little better structured than diff which has the following format. 

"file number": Differing line numbers
                       Differing lines 

Wonder what command to use to compare 4 files :-) 

No comments:

Post a Comment