1.8 Compare text files

To demonstrate the work of the commands, we will create three files with the following contents:

  #	f1.txt               f2.txt               f3.txt
  1	1 some		     1 some		  1 some
  2	2 simple	     2 symple		  2 symple
  3	3 text		     3 text		  3 text
  4			     4 add		  5 line
  5			     5 line

diff
The diff utility is a data comparison tool that calculates and displays the differences between two files. The command displays the changes made in a standard format, such that both humans and machines can understand the changes and apply them: given one file and the changes, the other file can be created. Typically, diff is used to show the changes between two versions of the same file.

# diff f1.txt f2.txt
2c2
< 2 simple
---
> 2 symple
3a4,5
> 4 add
> 5 line
# diff f2.txt f3.txt
4d3
< 4 add

In traditional output format, a stands for added, d for deleted and c for changed. Line numbers of the original file appear before a/d/c and those of the modified file appear after. The lesss-than and greater-than signs (at the beginning of lines that are added, deleted or changed) indicate which file the lines appear in. Addition lines are added to the original file to appear in the new file. Deletion lines are deleted from the original file to be missing in the new file.

By default, lines common to both files are not shown. Lines that have moved are shown as added at their new location and as deleted from their old location. However, in some distribution diff tools highlight moved lines.
Wikipedia: Diff utility

diff3
This utility can compare three files and show any differences among them. It can also merge files, implementing a three-way merge.

# diff3 f2.txt f1.txt f3.txt
====2
1:2c
3:2c
  2 symple
2:2c
  2 simple
====
1:4,5c
  4 add
  5 line
2:3a
3:4c
  5 line

This is like subtracting the file f1.txt from the file f2.txt and adding the result to the file f3.txt, or as merging into f2.txt the changes that would turn f1.txt into f3.txt.
Wikipedia: diff3

patch
The patch is a Unix program that updates text files according to instructions contained in a separate file, called a patch file. The patch file (also called a patch for short) is a text file that consists of a list of differences and is produced by running the related diff program with the original and updated file as arguments. Updating files with patch is often referred to as applying the patch or simply patching the files.

Lets create sample file:

# diff -uaN f1.txt f2.txt > p1.patch
# cat p1.patch
--- f1.txt	2017-06-30 04:01:12.284878223 -0400
+++ f2.txt	2017-06-30 06:29:27.825299201 -0400
@@ -1,3 +1,5 @@
 1 some
-2 simple
+2 symple
 3 text
+4 add
+5 line

Now we can use the patchfile to change a file f1.txt

# patch < p1.patch 
patching file f1.txt

As a result, we get a file f1.txt with the contents as in the file f2.txt. To cancel changes, just run the following command:

# patch -R < p1.patch 

vimdiff
This utility starts Vim on two (or three or four) files. Each file gets its own window. The differences between the files are highlighted. This is a nice way to inspect changes and to move changes from one version to another version of the same file.

To switch between files, use the keyboard shortcut Ctrl+w

# vimdiff f1.txt f2.txt

To fast receive or send changes type do - (diff obtain), dp - (diff put).

pr
Small text files can sometimes be conveniently compared using the command pr. The contents of files can be output to standard output in the form of columns.

# pr -m -n -t f1.txt f2.txt
1	1 some				1 some
2	2 simple			2 symple
3	3 text				3 text
4					4 add
5					5 line

CentOS 7

By default include commands:diff ,diff3 ,vimdiff and pr.

For install patch use following command:

# yum install patch

openSUSE Leap 42.3

By default include commands:diff ,diff3 ,vimdiff and pr.

For install patch use following command:

> sudo zypper in patch

Ubuntu 17.04

By default include commands:diff ,diff3 ,patch and pr.

For install vimdiff use following command:

$ sudo apt install vim
Publication/Release Date: Jul 01, 2017

Advertisement