📄 diff.texi
字号:
with the @option{-W @var{columns}} or @option{--width=@var{columns}}option. The output is split into two halves of equal width, separated by asmall gutter to mark differences; the right half is aligned to a tabstop so that tabs line up. Input lines that are too long to fit in halfof an output line are truncated for output.The @option{--left-column} option prints only the left column of twocommon lines. The @option{--suppress-common-lines} option suppressescommon lines entirely.@node Example Side by Side@subsection An Example of Side by Side FormatHere is the output of the command @samp{diff -y -W 72 lao tzu}(@pxref{Sample diff Input}, for the complete contents of the two files).@exampleThe 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 HeThe 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!@end example@node Normal@section Showing Differences Without Context@cindex normal output format@cindex @samp{<} output formatThe ``normal'' @command{diff} output format shows each hunk of differenceswithout any surrounding context. Sometimes such output is the clearestway to see how lines have changed, without the clutter of nearbyunchanged lines (although you can get similar results with the contextor unified formats by using 0 lines of context). However, this formatis no longer widely used for sending out patches; for that purpose, thecontext format (@pxref{Context Format}) and the unified format(@pxref{Unified Format}) are superior. Normal format is the default forcompatibility with older versions of @command{diff} and the @acronym{POSIX}standard. Use the @option{--normal} option to select this outputformat explicitly.@menu* Example Normal:: Sample output in the normal format.* Detailed Normal:: A detailed description of normal output format.@end menu@node Example Normal@subsection An Example of Normal FormatHere is the output of the command @samp{diff lao tzu}(@pxref{Sample diff Input}, for the complete contents of the two files).Notice that it shows only the lines that are different between the twofiles.@example1,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!@end example@node Detailed Normal@subsection Detailed Description of Normal FormatThe normal output format consists of one or more hunks of differences;each hunk shows one area where the files differ. Normal format hunkslook like this:@example@var{change-command}< @var{from-file-line}< @var{from-file-line}@dots{}---> @var{to-file-line}> @var{to-file-line}@dots{}@end exampleThere are three types of change commands. Each consists of a linenumber or comma-separated range of lines in the first file, a singlecharacter indicating the kind of change to make, and a line number orcomma-separated range of lines in the second file. All line numbers arethe original line numbers in each file. The types of change commandsare:@table @samp@item @var{l}a@var{r}Add the lines in range @var{r} of the second file after line @var{l} ofthe first file. For example, @samp{8a12,15} means append lines 12--15of file 2 after line 8 of file 1; or, if changing file 2 into file 1,delete lines 12--15 of file 2.@item @var{f}c@var{t}Replace the lines in range @var{f} of the first file with lines in range@var{t} of the second file. This is like a combined add and delete, butmore compact. For example, @samp{5,7c8,10} means change lines 5--7 offile 1 to read as lines 8--10 of file 2; or, if changing file 2 intofile 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1.@item @var{r}d@var{l}Delete the lines in range @var{r} from the first file; line @var{l} is wherethey would have appeared in the second file had they not been deleted.For example, @samp{5,7d3} means delete lines 5--7 of file 1; or, ifchanging file 2 into file 1, append lines 5--7 of file 1 after line 3 offile 2.@end table@node Scripts@section Making Edit Scripts@cindex script output formatsSeveral output modes produce command scripts for editing @var{from-file}to produce @var{to-file}.@menu* ed Scripts:: Using @command{diff} to produce commands for @command{ed}.* Forward ed:: Making forward @command{ed} scripts.* RCS:: A special @command{diff} output format used by @acronym{RCS}.@end menu@node ed Scripts@subsection @command{ed} Scripts@cindex @command{ed} script output format@command{diff} can produce commands that direct the @command{ed} text editorto change the first file into the second file. Long ago, this was theonly output mode that was suitable for editing one file into anotherautomatically; today, with @command{patch}, it is almost obsolete. Use the@option{-e} or @option{--ed} option to select this output format.Like the normal format (@pxref{Normal}), this output format does notshow any context; unlike the normal format, it does not include theinformation necessary to apply the diff in reverse (to produce the firstfile if all you have is the second file and the diff).If the file @file{d} contains the output of @samp{diff -e old new}, thenthe command @samp{(cat d && echo w) | ed - old} edits @file{old} to makeit a copy of @file{new}. More generally, if @file{d1}, @file{d2},@dots{}, @file{dN} contain the outputs of @samp{diff -e old new1},@samp{diff -e new1 new2}, @dots{}, @samp{diff -e newN-1 newN},respectively, then the command @samp{(cat d1 d2 @dots{} dN && echo w) |ed - old} edits @file{old} to make it a copy of @file{newN}.@menu* Example ed:: A sample @command{ed} script.* Detailed ed:: A detailed description of @command{ed} format.@end menu@node Example ed@subsubsection Example @command{ed} ScriptHere is the output of @samp{diff -e lao tzu} (@pxref{Samplediff Input}, for the complete contents of the two files):@example11aThey both may be called deep and profound.Deeper and more profound,The door of all subtleties!.4cThe named is the mother of all things..1,2d@end example@node Detailed ed@subsubsection Detailed Description of @command{ed} FormatThe @command{ed} output format consists of one or more hunks ofdifferences. The changes closest to the ends of the files come first sothat commands that change the number of lines do not affect how@command{ed} interprets line numbers in succeeding commands. @command{ed}format hunks look like this:@example@var{change-command}@var{to-file-line}@var{to-file-line}@dots{}.@end exampleBecause @command{ed} uses a single period on a line to indicate theend of input, @acronym{GNU} @command{diff} protects lines of changesthat contain a single period on a line by writing two periods instead,then writing a subsequent @command{ed} command to change the twoperiods into one. The @command{ed} format cannot represent anincomplete line, so if the second file ends in a changed incompleteline, @command{diff} reports an error and then pretends that a newlinewas appended.There are three types of change commands. Each consists of a linenumber or comma-separated range of lines in the first file and a singlecharacter indicating the kind of change to make. All line numbers arethe original line numbers in the file. The types of change commandsare:@table @samp@item @var{l}aAdd text from the second file after line @var{l} in the first file. Forexample, @samp{8a} means to add the following lines after line 8 of file1.@item @var{r}cReplace the lines in range @var{r} in the first file with the followinglines. Like a combined add and delete, but more compact. For example,@samp{5,7c} means change lines 5--7 of file 1 to read as the text file2.@item @var{r}dDelete the lines in range @var{r} from the first file. For example,@samp{5,7d} means delete lines 5--7 of file 1.@end table@node Forward ed@subsection Forward @command{ed} Scripts@cindex forward @command{ed} script output format@command{diff} can produce output that is like an @command{ed} script, butwith hunks in forward (front to back) order. The format of the commandsis also changed slightly: command characters precede the lines theymodify, spaces separate line numbers in ranges, and no attempt is madeto disambiguate hunk lines consisting of a single period. Like@command{ed} format, forward @command{ed} format cannot represent incompletelines.Forward @command{ed} format is not very useful, because neither @command{ed}nor @command{patch} can apply diffs in this format. It exists mainly forcompatibility with older versions of @command{diff}. Use the @option{-f} or@option{--forward-ed} option to select it.@node RCS@subsection @acronym{RCS} Scripts@cindex @acronym{RCS} script output formatThe @acronym{RCS} output format is designed specifically for use bythe Revision Control System, which is a set of free programs used fororganizing different versions and systems of files. Use the@option{-n} or @option{--rcs} option to select this output format. Itis like the forward @command{ed} format (@pxref{Forward ed}), but itcan represent arbitrary changes to the contents of a file because itavoids the forward @command{ed} format's problems with linesconsisting of a single period and with incomplete lines. Instead ofending text sections with a line consisting of a single period, eachcommand specifies the number of lines it affects; a combination of the@samp{a} and @samp{d} commands are used instead of @samp{c}. Also, ifthe second file ends in a changed incomplete line, then the outputalso ends in an incomplete line.Here is the output of @samp{diff -n lao tzu} (@pxref{Samplediff Input}, for the complete contents of the two files):@exampled1 2d4 1a4 2The named is the mother of all things.a11 3They both may be called deep and profound.Deeper and more profound,The door of all subtleties!@end example@node If-then-else@section Merging Files with If-then-else@cindex merged output format@cindex if-then-else output format@cindex C if-then-else output format@cindex @command{ifdef} output formatYou can use @command{diff} to merge two files of C source code. The outputof @command{diff} in this format contains all the lines of both files.Lines common to both files are output just once; the differing parts areseparated by the C preprocessor directives @code{#ifdef @var{name}} or@code{#ifndef @var{name}}, @code{#else}, and @code{#endif}. Whencompiling the output, you select which version to use by either definingor leaving undefined the macro @var{name}.To merge two files, use @command{diff} with the @option{-D @var{name}} or@option{--ifdef=@var{name}} option. The argument @var{name} is the Cpreprocessor identifier to use in the @code{#ifdef} and @code{#ifndef}directives.For example, if you change an instance of @code{wait (&s)} to@code{waitpid (-1, &s, 0)} and then merge the old and new files withthe @option{--ifdef=HAVE_WAITPID} option, then the affected part of your codemight look like this:@example 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);@end exampleYou can specify formats for languages other than C by using line groupformats and line formats, as described in the next sections.@menu* Line Group Formats:: Formats for general if-then-else line groups.* Line Formats:: Formats for each line in a line group.* Example If-then-else:: Sample if-then-else format output.* Detailed If-then-else:: A detailed description of if-then-else format.@end menu@node Line Group Formats@subsection Line Group Formats@cindex line group formats@cindex formats for if-then-else line groupsLine group formats let you specify formats suitable for manyapplications that allow if-then-else input, including programminglanguages and text formatting languages. A line group format specifiesthe output format for a contiguous group of similar lines.For example, the following command compares the TeX files @file{old}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -