diff
Output Formats
diff
has several mutually exclusive options for output format.
The following sections describe each format, illustrating how
diff
reports the differences between two sample input files.
diff
input files for examples.
Here are two sample files that we will use in numerous examples to
illustrate the output of diff
and how various options can change
it.
This is the file `lao':
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 outcome. The two are the same, But after they are produced, they have different names.
This is the file `tzu':
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 outcome. The two are the same, But after they are produced, they have different names. They both may be called deep and profound. Deeper and more profound, The door of all subtleties!
In this example, the first hunk contains just the first two lines of `lao', the second hunk contains the fourth line of `lao' opposing the second and third lines of `tzu', and the last hunk contains just the last three lines of `tzu'.
The "normal" diff
output format shows each hunk of differences
without any surrounding context. Sometimes such output is the clearest
way to see how lines have changed, without the clutter of nearby
unchanged lines (although you can get similar results with the context
or unified formats by using 0 lines of context). However, this format
is no longer widely used for sending out patches; for that purpose, the
context format (see section Context Format) and the unified format
(see section Unified Format) are superior. Normal format is the default for
compatibility with older versions of diff
and the Posix standard.
The normal output format consists of one or more hunks of differences; each hunk shows one area where the files differ. Normal format hunks look like this:
change-command < from-file-line < from-file-line... --- > to-file-line > to-file-line...
There are three types of change commands. Each consists of a line number or comma-separated range of lines in the first file, a single character indicating the kind of change to make, and a line number or comma-separated range of lines in the second file. All line numbers are the original line numbers in each file. The types of change commands are:
Here is the output of the command `diff lao tzu' (see section Two Sample Input Files, for the complete contents of the two files). Notice that it shows only the lines that are different between the two files.
1,2d0 < The Way that can be told of is not the eternal Way; < The name that can be named is not the eternal name. 4c2,3 < The Named is the mother of all things. --- > The named is the mother of all things. > 11a11,13 > They both may be called deep and profound. > Deeper and more profound, > The door of all subtleties!
Usually, when you are looking at the differences between files, you will also want to see the parts of the files near the lines that differ, to help you understand exactly what has changed. These nearby parts of the files are called the context.
GNU diff
provides two output formats that show context around the
differing lines: context format and unified format. It can
optionally show in which function or section of the file the differing
lines are found.
If you are distributing new versions of files to other people in the
form of diff
output, you should use one of the output formats
that show context so that they can apply the diffs even if they have
made small changes of their own to the files. patch
can apply
the diffs in this case by searching in the files for the lines of
context around the differing lines; if those lines are actually a few
lines away from where the diff says they are, patch
can adjust
the line numbers accordingly and still apply the diff correctly.
See section Applying Imperfect Patches, for more information on using patch
to apply
imperfect diffs.
The context output format shows several lines of context around the lines that differ. It is the standard format for distributing updates to source code.
To select this output format, use the `-C lines',
`--context[=lines]', or `-c' option. The
argument lines that some of these options take is the number of
lines of context to show. If you do not specify lines, it
defaults to three. For proper operation, patch
typically needs
at least two lines of context.
The context output format starts with a two-line header, which looks like this:
*** from-file from-file-modification-time --- to-file to-file-modification time
You can change the header's content with the `-L label' or `--label=label' option; see section Showing Alternate File Names.
Next come one or more hunks of differences; each hunk shows one area where the files differ. Context format hunks look like this:
*************** *** from-file-line-range **** from-file-line from-file-line... --- to-file-line-range ---- to-file-line to-file-line...
The lines of context around the lines that differ start with two space characters. The lines that differ between the two files start with one of the following indicator characters, followed by a space character:
If all of the changes in a hunk are insertions, the lines of from-file are omitted. If all of the changes are deletions, the lines of to-file are omitted.
Here is the output of `diff -c lao tzu' (see section Two Sample Input Files, for the complete contents of the two files). Notice that up to three lines that are not different are shown around each line that is different; they are the context lines. Also notice that the first two hunks have run together, because their contents overlap.
*** lao Sat Jan 26 23:30:39 1991 --- tzu Sat Jan 26 23:30:50 1991 *************** *** 1,7 **** - 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, --- 1,6 ---- 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, *************** *** 9,11 **** --- 8,13 ---- The two are the same, But after they are produced, they have different names. + They both may be called deep and profound. + Deeper and more profound, + The door of all subtleties!
Here is the output of `diff --context=1 lao tzu' (see section Two Sample Input Files, for the complete contents of the two files). Notice that at most one context line is reported here.
*** lao Sat Jan 26 23:30:39 1991 --- tzu Sat Jan 26 23:30:50 1991 *************** *** 1,5 **** - 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, --- 1,4 ---- The Nameless is the origin of Heaven and Earth; ! The named is the mother of all things. ! Therefore let there always be non-being, *************** *** 11 **** --- 10,13 ---- they have different names. + They both may be called deep and profound. + Deeper and more profound, + The door of all subtleties!
The unified output format is a variation on the context format that is more compact because it omits redundant context lines. To select this output format, use the `-U lines', `--unified[=lines]', or `-u' option. The argument lines is the number of lines of context to show. When it is not given, it defaults to three.
At present, only GNU diff
can produce this format and only GNU
patch
can automatically apply diffs in this format. For proper
operation, patch
typically needs at least two lines of context.
The unified output format starts with a two-line header, which looks like this:
--- from-file from-file-modification-time +++ to-file to-file-modification-time
You can change the header's content with the `-L label' or `--label=label' option; see See section Showing Alternate File Names.
Next come one or more hunks of differences; each hunk shows one area where the files differ. Unified format hunks look like this:
@@ from-file-range to-file-range @@ line-from-either-file line-from-either-file...
The lines common to both files begin with a space character. The lines that actually differ between the two files have one of the following indicator characters in the left column:
Here is the output of the command `diff -u lao tzu' (see section Two Sample Input Files, for the complete contents of the two files):
--- lao Sat Jan 26 23:30:39 1991 +++ tzu Sat Jan 26 23:30:50 1991 @@ -1,7 +1,6 @@ -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. +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, @@ -9,3 +8,6 @@ The two are the same, But after they are produced, they have different names. +They both may be called deep and profound. +Deeper and more profound, +The door of all subtleties!
Sometimes you might want to know which part of the files each change
falls in. If the files are source code, this could mean which function
was changed. If the files are documents, it could mean which chapter or
appendix was changed. GNU diff
can show this by displaying the
nearest section heading line that precedes the differing lines. Which
lines are "section headings" is determined by a regular expression.
To show in which sections differences occur for files that are not
source code for C or similar languages, use the `-F regexp'
or `--show-function-line=regexp' option. diff
considers lines that match the argument regexp to be the beginning
of a section of the file. Here are suggested regular expressions for
some common languages:
This option does not automatically select an output format; in order to use it, you must select the context format (see section Context Format) or unified format (see section Unified Format). In other output formats it has no effect.
The `-F' and `--show-function-line' options find the nearest
unchanged line that precedes each hunk of differences and matches the
given regular expression. Then they add that line to the end of the
line of asterisks in the context format, or to the `@@' line in
unified format. If no matching line exists, they leave the output for
that hunk unchanged. If that line is more than 40 characters long, they
output only the first 40 characters. You can specify more than one
regular expression for such lines; diff
tries to match each line
against each regular expression, starting with the last one given. This
means that you can use `-p' and `-F' together, if you wish.
To show in which functions differences occur for C and similar languages, you can use the `-p' or `--show-c-function' option. This option automatically defaults to the context output format (see section Context Format), with the default number of lines of context. You can override that number with `-C lines' elsewhere in the command line. You can override both the format and the number with `-U lines' elsewhere in the command line.
The `-p' and `--show-c-function' options are equivalent to
`-F'^[_a-zA-Z$]'' if the unified format is specified, otherwise
`-c -F'^[_a-zA-Z$]'' (see section Showing Lines That Match Regular Expressions). GNU diff
provides them for the sake of convenience.
If you are comparing two files that have meaningless or uninformative
names, you might want diff
to show alternate names in the header
of the context and unified output formats. To do this, use the `-L
label' or `--label=label' option. The first time
you give this option, its argument replaces the name and date of the
first file in the header; the second time, its argument replaces the
name and date of the second file. If you give this option more than
twice, diff
reports an error. The `-L' option does not
affect the file names in the pr
header when the `-l' or
`--paginate' option is used (see section Paginating diff
Output).
Here are the first two lines of the output from `diff -C2 -Loriginal -Lmodified lao tzu':
*** original --- modified
diff
can produce a side by side difference listing of two files.
The files are listed in two columns with a gutter between them. The
gutter contains one of the following markers:
Normally, an output line is incomplete if and only if the lines that it contains are incomplete; See section Incomplete Lines. However, when an output line represents two differing lines, one might be incomplete while the other is not. In this case, the output line is complete, but its the gutter is marked `\' if the first line is incomplete, `/' if the second line is.
Side by side format is sometimes easiest to read, but it has limitations. It generates much wider output than usual, and truncates lines that are too long to fit. Also, it relies on lining up output more heavily than usual, so its output looks particularly bad if you use varying width fonts, nonstandard tab stops, or nonprinting characters.
You can use the sdiff
command to interactively merge side by side
differences. See section Interactive Merging with sdiff
, for more information on merging files.
The `-y' or `--side-by-side' option selects side by side format. Because side by side output lines contain two input lines, they are wider than usual. They are normally 130 columns, which can fit onto a traditional printer line. You can set the length of output lines with the `-W columns' or `--width=columns' option. The output line is split into two halves of equal length, separated by a small gutter to mark differences; the right half is aligned to a tab stop so that tabs line up. Input lines that are too long to fit in half of an output line are truncated for output.
The `--left-column' option prints only the left column of two common lines. The `--suppress-common-lines' option suppresses common lines entirely.
Here is the output of the command `diff -y -W 72 lao tzu' (see section Two Sample Input Files, for the complete contents of the two files).
The Way that can be told of is n < The name that can be named is no < The Nameless is the origin of He The Nameless is the origin of He The Named is the mother of all t | The named is the mother of all t > Therefore let there always be no Therefore let there always be no so we may see their subtlety, so we may see their subtlety, And let there always be being, And let there always be being, so we may see their outcome. so we may see their outcome. The two are the same, The two are the same, But after they are produced, But after they are produced, they have different names. they have different names. > They both may be called deep and > Deeper and more profound, > The door of all subtleties!
Several output modes produce command scripts for editing from-file to produce to-file.
diff
to produce commands for ed
.
ed
scripts.
diff
output format used by RCS.
ed
Scripts
diff
can produce commands that direct the ed
text editor
to change the first file into the second file. Long ago, this was the
only output mode that was suitable for editing one file into another
automatically; today, with patch
, it is almost obsolete. Use the
`-e' or `--ed' option to select this output format.
Like the normal format (see section Showing Differences Without Context), this output format does not show any context; unlike the normal format, it does not include the information necessary to apply the diff in reverse (to produce the first file if all you have is the second file and the diff).
If the file `d' contains the output of `diff -e old new', then the command `(cat d && echo w) | ed - old' edits `old' to make it a copy of `new'. More generally, if `d1', `d2', ..., `dN' contain the outputs of `diff -e old new1', `diff -e new1 new2', ..., `diff -e newN-1 newN', respectively, then the command `(cat d1 d2 ... dN && echo w) | ed - old' edits `old' to make it a copy of `newN'.
ed
format.
ed
script.
ed
Format
The ed
output format consists of one or more hunks of
differences. The changes closest to the ends of the files come first so
that commands that change the number of lines do not affect how
ed
interprets line numbers in succeeding commands. ed
format hunks look like this:
change-command to-file-line to-file-line... .
Because ed
uses a single period on a line to indicate the end of
input, GNU diff
protects lines of changes that contain a single
period on a line by writing two periods instead, then writing a
subsequent ed
command to change the two periods into one. The
ed
format cannot represent an incomplete line, so if the second
file ends in a changed incomplete line, diff
reports an error and
then pretends that a newline was appended.
There are three types of change commands. Each consists of a line number or comma-separated range of lines in the first file and a single character indicating the kind of change to make. All line numbers are the original line numbers in the file. The types of change commands are:
ed
ScriptHere is the output of `diff -e lao tzu' (see section Two Sample Input Files, for the complete contents of the two files):
11a They both may be called deep and profound. Deeper and more profound, The door of all subtleties! . 4c The named is the mother of all things. . 1,2d
ed
Scripts
diff
can produce output that is like an ed
script, but
with hunks in forward (front to back) order. The format of the commands
is also changed slightly: command characters precede the lines they
modify, spaces separate line numbers in ranges, and no attempt is made
to disambiguate hunk lines consisting of a single period. Like
ed
format, forward ed
format cannot represent incomplete
lines.
Forward ed
format is not very useful, because neither ed
nor patch
can apply diffs in this format. It exists mainly for
compatibility with older versions of diff
. Use the `-f' or
`--forward-ed' option to select it.
The RCS output format is designed specifically for use by the Revision
Control System, which is a set of free programs used for organizing
different versions and systems of files. Use the `-n' or
`--rcs' option to select this output format. It is like the
forward ed
format (see section Forward ed
Scripts), but it can represent
arbitrary changes to the contents of a file because it avoids the
forward ed
format's problems with lines consisting of a single
period and with incomplete lines. Instead of ending text sections with
a line consisting of a single period, each command specifies the number
of lines it affects; a combination of the `a' and `d'
commands are used instead of `c'. Also, if the second file ends
in a changed incomplete line, then the output also ends in an
incomplete line.
Here is the output of `diff -n lao tzu' (see section Two Sample Input Files, for the complete contents of the two files):
d1 2 d4 1 a4 2 The named is the mother of all things. a11 3 They both may be called deep and profound. Deeper and more profound, The door of all subtleties!
You can use diff
to merge two files of C source code. The output
of diff
in this format contains all the lines of both files.
Lines common to both files are output just once; the differing parts are
separated by the C preprocessor directives #ifdef name
or
#ifndef name
, #else
, and #endif
. When
compiling the output, you select which version to use by either defining
or leaving undefined the macro name.
To merge two files, use diff
with the `-D name' or
`--ifdef=name' option. The argument name is the C
preprocessor identifier to use in the #ifdef
and #ifndef
directives.
For example, if you change an instance of wait (&s)
to
waitpid (-1, &s, 0)
and then merge the old and new files with
the `--ifdef=HAVE_WAITPID' option, then the affected part of your code
might look like this:
do { #ifndef HAVE_WAITPID if ((w = wait (&s)) < 0 && errno != EINTR) #else /* HAVE_WAITPID */ if ((w = waitpid (-1, &s, 0)) < 0 && errno != EINTR) #endif /* HAVE_WAITPID */ return w; } while (w != child);
You can specify formats for languages other than C by using line group formats and line formats, as described in the next sections.
Line group formats let you specify formats suitable for many applications that allow if-then-else input, including programming languages and text formatting languages. A line group format specifies the output format for a contiguous group of similar lines.
For example, the following command compares the TeX files `old' and `new', and outputs a merged file in which old regions are surrounded by `\begin{em}'-`\end{em}' lines, and new regions are surrounded by `\begin{bf}'-`\end{bf}' lines.
diff \ --old-group-format='\begin{em} %<\end{em} ' \ --new-group-format='\begin{bf} %>\end{bf} ' \ old new
The following command is equivalent to the above example, but it is a little more verbose, because it spells out the default line group formats.
diff \ --old-group-format='\begin{em} %<\end{em} ' \ --new-group-format='\begin{bf} %>\end{bf} ' \ --unchanged-group-format='%=' \ --changed-group-format='\begin{em} %<\end{em} \begin{bf} %>\end{bf} ' \ old new
Here is a more advanced example, which outputs a diff listing with headers containing line numbers in a "plain English" style.
diff \ --unchanged-group-format=" \ --old-group-format='-------- %dn line%(n=1?:s) deleted at %df: %<' \ --new-group-format='-------- %dN line%(N=1?:s) added after %de: %>' \ --changed-group-format='-------- %dn line%(n=1?:s) changed at %df: %<-------- to: %>' \ old new
To specify a line group format, use diff
with one of the options
listed below. You can specify up to four line group formats, one for
each kind of line group. You should quote format, because it
typically contains shell metacharacters.
In a line group format, ordinary characters represent themselves; conversion specifications start with `%' and have one of the following forms.
printf
conversion specification and n is one
of the following letters, stands for n's value formatted with F.
printf
conversion specification can be `%d',
`%o', `%x', or `%X', specifying decimal, octal,
lower case hexadecimal, or upper case hexadecimal output
respectively. After the `%' the following options can appear in
sequence: a `-' specifying left-justification; an integer
specifying the minimum field width; and a period followed by an
optional integer specifying the minimum number of digits.
For example, `%5dN' prints the number of new lines in the group
in a field of width 5 characters, using the printf
format "%5d"
.
Line formats control how each line taken from an input file is output as part of a line group in if-then-else format.
For example, the following command outputs text with a one-column change indicator to the left of the text. The first column of output is `-' for deleted lines, `|' for added lines, and a space for unchanged lines. The formats contain newline characters where newlines are desired on output.
diff \ --old-line-format='-%l ' \ --new-line-format='|%l ' \ --unchanged-line-format=' %l ' \ old new
To specify a line format, use one of the following options. You should quote format, since it often contains shell metacharacters.
In a line format, ordinary characters represent themselves; conversion specifications start with `%' and have one of the following forms.
printf
conversion specification,
stands for the line number formatted with F.
For example, `%.5dn' prints the line number using the
printf
format "%.5d"
. See section Line Group Formats, for
more about printf conversion specifications.
The default line format is `%l' followed by a newline character.
If the input contains tab characters and it is important that they line up on output, you should ensure that `%l' or `%L' in a line format is just after a tab stop (e.g. by preceding `%l' or `%L' with a tab character), or you should use the `-t' or `--expand-tabs' option.
Taken together, the line and line group formats let you specify many
different formats. For example, the following command uses a format
similar to diff
's normal format. You can tailor this command
to get fine control over diff
's output.
diff \ --old-line-format='< %l ' \ --new-line-format='> %l ' \ --old-group-format='%df%(f=l?:,%dl)d%dE %<' \ --new-group-format='%dea%dF%(F=L?:,%dL) %>' \ --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL) %<--- %>' \ --unchanged-group-format=" \ old new
For lines common to both files, diff
uses the unchanged line
group format. For each hunk of differences in the merged output
format, if the hunk contains only lines from the first file,
diff
uses the old line group format; if the hunk contains only
lines from the second file, diff
uses the new group format;
otherwise, diff
uses the changed group format.
The old, new, and unchanged line formats specify the output format of lines from the first file, lines from the second file, and lines common to both files, respectively.
The option `--ifdef=name' is equivalent to the following sequence of options using shell syntax:
--old-group-format='#ifndef name %<#endif /* not name */ ' \ --new-group-format='#ifdef name %>#endif /* name */ ' \ --unchanged-group-format='%=' \ --changed-group-format='#ifndef name %<#else /* name */ %>#endif /* name */ '
You should carefully check the diff
output for proper nesting.
For example, when using the the `-D name' or
`--ifdef=name' option, you should check that if the
differing lines contain any of the C preprocessor directives
`#ifdef', `#ifndef', `#else', `#elif', or
`#endif', they are nested properly and match. If they don't, you
must make corrections manually. It is a good idea to carefully check
the resulting code anyway to make sure that it really does what you
want it to; depending on how the input files were produced, the output
might contain duplicate or otherwise incorrect code.
The patch
`-D name' option behaves just like
the diff
`-D name' option, except it operates on
a file and a diff to produce a merged file; See section Options to patch
.
Here is the output of `diff -DTWO lao tzu' (see section Two Sample Input Files, for the complete contents of the two files):
#ifndef TWO The Way that can be told of is not the eternal Way; The name that can be named is not the eternal name. #endif /* not TWO */ The Nameless is the origin of Heaven and Earth; #ifndef TWO The Named is the mother of all things. #else /* TWO */ The named is the mother of all things. #endif /* TWO */ Therefore let there always be non-being, so we may see their subtlety, And let there always be being, so we may see their outcome. The two are the same, But after they are produced, they have different names. #ifdef TWO They both may be called deep and profound. Deeper and more profound, The door of all subtleties! #endif /* TWO */