Search

Comparing two files word by word


wdiff: Utility to comapre two files word by word.

When comapring two files, the usual diff command compares line by line, but if we wanted the comparision to happen word by word then diff command would not be helpful.

The wdiff command will pick up the exact words that do not match between the lines.

wdiff is not present by default and needs to be installed. Run the following command to install wdiff package.

sudo apt-get install wdiff

or you can select wdiff from the synaptic package manager.

Let us take two files

one:
This is the first file

and

two:
This is the second file


If we run the command diff on these two files

$ diff one two 

1c1
< This is the first file
---
> This is the second file


Note:The output of diff is explained in the post "Comparing files using diff

The above output does not tell us which are the mismatching words . Let us try wdiff and see the output

$ wdiff one two 
This is the [-first-] {+second+} file

In the output braces are put around the differing words. In the above example words [-first-], {+second+} are put in braces. The words with square braces of the kind "[" are the words of the first file and the words with curly braces of the kind "{" belong to the second file.

The "-" around the first set of words signify that if these set of words are removed from the first file and the second set of words with "+" around them are added to it, we will get the second file.

If there are multiple lines in the file the differing words in each line is printed out loine by line.

if we pass the option -3 to wdiff it will output only the differing words instead of the whole lines

$wdiff -3 one two 
============================================================
 [-first line-] {+second+}
============================================================



No comments:

Post a Comment