Comparing and Merging Files - 6. Comparing Three Files

Go to the first, previous, next, last section, table of contents.

# 6. Comparing Three Files

Use the program `diff3` to compare three files and show any differences among them. (`diff3` can also merge files; see section 7. Merging From a Common Ancestor).

The "normal" `diff3` output format shows each hunk of differences without surrounding context. Hunks are labeled depending on whether they are two-way or three-way, and lines are annotated by their location in the input files.

See section 13. Invoking `diff3`, for more information on how to run `diff3`.

## 6.1 A Third Sample Input File

Here is a third sample file that will be used in examples to illustrate the output of `diff3` and how various options can change it. The first two files are the same that we used for `diff` (see section 2.1 Two Sample Input Files). This is the third sample file, called ``tao'`:

    The Way that can be told of is not the eternal Way;
    The name that can be named is not the eternal name.
    The Nameless is the origin of Heaven and Earth;
    The named is the mother of all things.

    Therefore let there always be non-being,
      so we may see their subtlety,
    And let there always be being,
      so we may see their result.
    The two are the same,
    But after they are produced,
      they have different names.

      -- The Way of Lao-Tzu, tr. Wing-tsit Chan

## 6.2 Detailed Description of `diff3` Normal Format

Each hunk begins with a line marked ``===='`. Three-way hunks have plain ``===='` lines, and two-way hunks have ``1'`, ``2'`, or ``3'` appended to specify which of the three input files differ in that hunk. The hunks contain copies of two or three sets of input lines each preceded by one or two commands identifying where the lines came from.

Normally, two spaces precede each copy of an input line to distinguish it from the commands. But with the ``-T'` or ``--initial-tab'` option, `diff3` uses a tab instead of two spaces; this lines up tabs correctly. See section 4.1 Preserving Tabstop Alignment, for more information.

Commands take the following forms:

``file:la'`
  This hunk appears after line l of file file, and contains no lines in that file. To edit this file to yield the other files, one must append hunk lines taken from the other files. For example, ``1:11a'` means that the hunk follows line 11 in the first file and contains no lines from that file.
``file:rc'`
  This hunk contains the lines in the range r of file file. The range r is a comma-separated pair of line numbers, or just one number if the range is a singleton. To edit this file to yield the other files, one must change the specified lines to be the lines taken from the other files. For example, ``2:11,13c'` means that the hunk contains lines 11 through 13 from the second file.

If the last line in a set of input lines is incomplete (see section 16. Incomplete Lines), it is distinguished on output from a full line by a following line that starts with ``\'`.

## 6.3 `diff3` Hunks

Groups of lines that differ in two or three of the input files are called diff3 hunks, by analogy with `diff` hunks (see section 1.1 Hunks). If all three input files differ in a `diff3` hunk, the hunk is called a three-way hunk; if just two input files differ, it is a two-way hunk.

As with `diff`, several solutions are possible. When comparing the files ``A'`, ``B'`, and ``C'`, `diff3` normally finds `diff3` hunks by merging the two-way hunks output by the two commands ``diff A B'` and ``diff A C'`. This does not necessarily minimize the size of the output, but exceptions should be rare.

For example, suppose ``F'` contains the three lines ``a'`, ``b'`, ``f'`, ``G'` contains the lines ``g'`, ``b'`, ``g'`, and ``H'` contains the lines ``a'`, ``b'`, ``h'`. ``diff3 F G H'` might output the following:

    ====2
    1:1c
    3:1c
      a
    2:1c
      g
    ====
    1:3c
      f
    2:3c
      g
    3:3c
      h

because it found a two-way hunk containing ``a'` in the first and third files and ``g'` in the second file, then the single line ``b'` common to all three files, then a three-way hunk containing the last line of each file.

## 6.4 An Example of `diff3` Normal Format

Here is the output of the command ``diff3 lao tzu tao'` (see section 6.1 A Third Sample Input File, for the complete contents of the files). Notice that it shows only the lines that are different among the three files.

    ====2
    1:1,2c
    3:1,2c
      The Way that can be told of is not the eternal Way;
      The name that can be named is not the eternal name.
    2:0a
    ====1
    1:4c
      The Named is the mother of all things.
    2:2,3c
    3:4,5c
      The named is the mother of all things.

    ====3
    1:8c
    2:7c
        so we may see their outcome.
    3:9c
        so we may see their result.
    ====
    1:11a
    2:11,13c
      They both may be called deep and profound.
      Deeper and more profound,
      The door of all subtleties!
    3:13,14c

        -- The Way of Lao-Tzu, tr. Wing-tsit Chan

Go to the first, previous, next, last section, table of contents.