概述
A word difference finder (and others)
Next: Overview,Up: (dir)
GNU wdiff
These Info pages document wdiff, a word difference finder, togetherwith some otherdiff related tools. Note that some tools documented here are considered experimental and maynot be part of everywdiff installation. See Experimental.
This is for release 1.0.1.
--- The Detailed Node Listing ---
The word difference finder
The multi-difference finder
The diff format converter
How mdiff differs
Experimental programs
Table of Contents
- GNU wdiff
- 1 Overview
- 2 The word difference finder
- 2.1 Invoking wdiff
- 2.2 Actual examples of wdiff usage
- 3 The multi-difference finder
- 3.1 Invoking mdiff
- 3.2 Resource considerations and efficiency
- 4 The diff format converter
- 4.1 Invoking unify
- 5 How mdiff differs
- 5.1 Differences with diff
- 5.2 Differences with wdiff
- 6 Experimental programs
- 6.1 History of the Experimental programs
Next: wdiff,Previous: Top,Up: Top
1 Overview
wdiff is a front end to diff for comparing files ona word per word basis. It works by creating two temporary files, oneword per line, and then executesdiff on these files. It collects thediff output and uses it to produce a nicer display of word differencesbetween the original files.
mdiff studies one or more input files altogether, and discoversblocks of items which repeat at more than one place. Items may be lines,words, or units defined by user. When in word mode,mdiff comparestwo files, finding which words have been deleted or added to the firstin order to create the second, which is useful when two texts differonly by a few words and paragraphs have been refilled. The program hasmany output formats and interacts well with terminals and pagers (notablywithless).
unify is able to convert context diffs to unidiff format, or theother way around. Some people just prefer one format and despise theother, it is a religious issue. This program brings peace back to Earth.
wdiff2 is intended as a replacement towdiff. It aims at supporting the same set of options, but usesmdiffinstead of diff as its backend.
wdiff, mdiff andwdiff2were written by François Pinard, whileunify has been contributed by Wayne Davison. Please report bugs towdiff-bugs@gnu.org. Include the version number, whichyou can find by running the program with--version. Please includein your message sufficient input to reproduce what you got, the output youindeed expected, and careful explanations about the nature of the problem.
Next: mdiff,Previous: Overview,Up: Top
2 The word difference finder
There are actually two programs for comparing files on a word per wordbasis. wdiff is a front-end to diff as found in theGNU diffutils package. It is quite mature. Its planned successor,wdiff2, is a front end to mdiff,and as experimental asmdiff itself. See Experimental.
A word is anything between whitespace. This is useful for comparing twotexts in which a few words have been changed and for which paragraphshave been refilled.
Next: wdiff Examples,Up: wdiff
2.1 Invoking wdiff
The programs wdiff and wdiff2 aim at providing thesame set of command line options. They are described below. Seewdiff Compatibility, for a list of differences.
wdiff option ... old_file new_file wdiff option ... -d [diff_file]
wdiff compares files old_file andnew_file andproduces an annotated copy of new_file on standard output. Theempty string or the string ‘-’ denotes standard input, but standardinput cannot be used twice in the same invocation. The complete path ofa file should be given, a directory name is not accepted. wdiffwill exit with a status of 0 if no differences were found, a status of 1if any differences were found, or a status of 2 for any error.
In this documentation, deleted text refers to text inold_file which is not innew_file, while inserted textrefers to text on new_file which is not inold_file.
wdiff supports the following command line options:
-
--help
-h
-
Print an informative help message describing the options.
--version
-v
-
Print the version number of
wdiff on the standard error output.
--no-deleted
-1
-
Avoid producing deleted words on the output. If neither
-1 or
-2 is selected, the original right margin may be exceeded forsome lines.
--no-inserted
-2
-
Avoid producing inserted words on the output. When this flag is given,the whitespace in the output is taken from
old_file instead of
new_file. If neither
-1 or
-2 is selected, theoriginal right margin may be exceeded for some lines.
--no-common
-3
-
Avoid producing common words on the output. When this option is notselected, common words and whitespace are taken from
new_file,unless option
-2 is given, in which case common words andwhitespace are rather taken from
old_file. When selected,differences are separated from one another by lines of dashes. Moreover, if this option is selected at the same time as
-1 or
-2, then none of the output will have any emphasis, i.e. no boldor underlining. Finally, if this option is not selected, but both
-1 and
-2 are, then sections of common words betweendifferences are segregated by lines of dashes.
--ignore-case
-i
-
Do not consider case difference while comparing words. Each lower caseletter is seen as identical to its upper case equivalent for the purposeof deciding if two words are the same.
--statistics
-s
-
On completion, for each file, the total number of words, the number ofcommon words between the files, the number of words deleted or insertedand the number of words that have changed is output. (A changed word isone that has been replaced or is part of a replacement.) Except for thetotal number of words, all of the numbers are followed by a percentagerelative to the total number of words in the file.
--auto-pager
-a
-
Some initiatives which were previously automatically taken in previousversions of
wdiff are now put under the control of this option. By using it, a pager is interposed whenever the
wdiff output isdirected to the user's terminal. Without this option, no pager will becalled, the user is then responsible for explicitly piping
wdiffoutput into a pager, if required.
The pager is selected by the value of the PAGER environmentvariable whenwdiff is run. If PAGER is not defined atrun time, then a default pager, selected at installation time, will beused instead. A defined but empty value ofPAGER means no pagerat all.
When a pager is interposed through the use of this option,one of the options -l or -t is also selected, depending onwhether the string ‘less’ appears in the pager's name or not.
It is often useful to define ‘wdiff’ as an alias for ‘wdiff-a’. However, thishides the normal wdiff behaviour. Thedefault behaviour may be restored simply by piping the output fromwdiff throughcat. This dissociates the output from theuser's terminal.
--printer
-p
-
Use over-striking to emphasize parts of the output. Each character of thedeleted text is underlined by writing an underscore ‘
_’ first,then a backspace and then the letter to be underlined. Each character of theinserted text is emboldened by writing it twice, with a backspace inbetween. This option is not selected by default.
--less-mode
-l
-
Use over-striking to emphasize parts of output. This option works asoption
-p, but also over-strikes whitespace associated withinserted text.
less shows such whitespace using reverse video. This option is not selected by default. However, it is automaticallyturned on whenever
wdiff launches the pager
less. Seeoption
-a.
This option is commonly used in conjunction with less:
wdiff -l old_file new_file | less
--terminal
-t
-
Force the production of
termcap
strings for emphasising parts ofoutput, even if the standard output is not associated with a terminal. The TERM environment variable must contain the name of a validtermcap
entry. If the terminal description permits, underliningis used for marking deleted text, while bold or reverse video is usedfor marking inserted text. This option is not selected by default. However, it is automatically turned on whenever wdiff launches apager, and it is known that the pager is not less. Seeoption -a.This option is commonly used when wdiff output is not redirected,but sent directly to the user terminal, as in:
wdiff -t old_file new_file
A common kludge uses wdiff together with the pagermore,as in:
wdiff -t old_file new_file | more
However, some versions of more use
termcap
emphasis fortheir own purposes, so strange interactions are possible.
--start-delete argument
-w argument
-
Use
argument as the
start delete string. This string willbe output prior to any sequence of deleted text, to mark where itstarts. By default, no start delete string is used unless there is noother means of distinguishing where such text starts; in this case thedefault start delete string is ‘
[-’.
--end-delete argument
-x argument
-
Use
argument as the
end delete string. This string will beoutput after any sequence of deleted text, to mark where it ends. Bydefault, no end delete string is used unless there is no other means ofdistinguishing where such text ends; in this case the default end deletestring is
-].
--start-insert argument
-y argument
-
Use
argument as the
start insert string. This string willbe output prior to any sequence of inserted text, to mark where itstarts. By default, no start insert string is used unless there is noother means of distinguishing where such text starts; in this case thedefault start insert string is ‘
{+’.
--end-insert argument
-z argument
-
Use
argument as the
end insert string. This string will beoutput after any sequence of inserted text, to mark where it ends. Bydefault, no end insert string is used unless there is no other means ofdistinguishing where such text ends; in this case the default end insertstring is ‘
+}’.
--avoid-wraps
-n
-
Avoid spanning the end of line while showing deleted or inserted text. Any single fragment of deleted or inserted text spanning many lines willbe considered as being made up of many smaller fragments not containinga newline. So deleted text, for example, will have an end delete stringat the end of each line, just before the new line, and a start deletestring at the beginning of the next line. A long paragraph of insertedtext will have each line bracketed between start insert and end insertstrings. This behaviour is not selected by default.
--diff-input
-d
-
Use single unified diff as input. If no input file is specified,standard input is used instead. This can be used to post-process diffsgenerated form other applications, like version control systems:
svn diff | wdiff -d
Note that options -p, -t, and-[wxyz] are notmutually exclusive. If you use a combination of them, you will merelyaccumulate the effect of each. Option-l is a variant of option-p.
Previous: wdiff invocation,Up: wdiff
2.2 Actual examples of wdiff usage
This section presents a few examples of usage, most of them have beencontributed bywdiff users.
- Change bars example.
- This example comes from a discussion withJoe Wells.
The following command produces a copy of new_file, shifted rightone space to accommodate change bars since the last revision, ignoringthose changes coming only from paragraph refilling. Any line with newor changed text will get a ‘|’ in column 1. However, deleted textis not shown nor marked.
wdiff -1n old_file new_file | sed -e 's/^/ /;/{+/s/^ /|/;s/{+//g;s/+}//g'
Here is how it works. Word differences are found, paying attention onlyto additions, as requested by option-1. For bigger changeswhich span line boundaries, the insert bracket strings are repeated oneach output line, as requested by option-n. This output is thenreformatted with ased script which shifts the text right twocolumns, turns the initial space into a bar only if there is some newtext on that line, then removes all insert bracket strings.
- This example comes from a discussion withJoe Wells.
- LaTeX example.
- This example has been provided bySteve Fisk.
The following uses LaTeX to put deleted text in boxes, and new textin double boxes:
wdiff -w "fbox{" -x "}" -y "fbox{fbox{" -z "}}" ...
works nicely.
- This example has been provided bySteve Fisk.
- troff example.
- This example comes from Paul Fox.
Using wdiff, with some troff-specific delimiters givesmuch better output. The delimiters I used:
wdiff -w's-5' -x's0' -y'fB' -z'fP' ...
This makes the pointsize of deletions 5 points smaller than normal, andemboldens insertions. Fantastic!
I experimented with:
wdiff -w'fI' -x'fP' -y'fB' -z'fP'
since that's more like the defaults you use for terminals or printers,but since I actually use italics for emphasis in my documents, I thoughtthe point size thing was clearer.
I tried it on code, and it works surprisingly well there, too...
- Marty Leisner says:
In the previous example, you had smaller text being taken out and boldface inserted. I had smaller text being taken out and larger text beinginserted, I'm using bold face for other things, so this is more clear.
wdiff -w 's-3' -x's0' -y's+3' -z's0'
- This example comes from Paul Fox.
- Colored output example.
- This example comes from Martin von Gagern.
If you like colored output, and your terminal supports ANSI escapesequences, you can use this invocation:
wdiff -n -w $'
- This example comes from Martin von Gagern.