📄 diff.texi
字号:
2. A line is breadthless length.-- Euclid, The Elements, I@end exampleNormally this option affects only lines that are completely empty, butif you also specify the @option{-b} or @option{--ignore-space-change}option, or the @option{-w} or @option{--ignore-all-space} option,lines are also affected if they look empty but contain white space.In other words, @option{-B} is equivalent to @samp{-I '^$'} bydefault, but it is equivalent to @option{-I '^[[:space:]]*$'} if@option{-b} or @option{-w} is also specified.@node Specified Lines@section Suppressing Differences Whose Lines All Match a Regular Expression@cindex regular expression suppressionTo ignore insertions and deletions of lines that match a@command{grep}-style regular expression, use the @option{-I@var{regexp}} or @option{--ignore-matching-lines=@var{regexp}} option.You should escaperegular expressions that contain shell metacharacters to prevent theshell from expanding them. For example, @samp{diff -I '^[[:digit:]]'} ignoresall changes to lines beginning with a digit.However, @option{-I} only ignores the insertion or deletion of lines thatcontain the regular expression if every changed line in the hunk---everyinsertion and every deletion---matches the regular expression. In otherwords, for each nonignorable change, @command{diff} prints the complete setof changes in its vicinity, including the ignorable ones.You can specify more than one regular expression for lines to ignore byusing more than one @option{-I} option. @command{diff} tries to match eachline against each regular expression.@node Case Folding@section Suppressing Case Differences@cindex case difference suppression@acronym{GNU} @command{diff} can treat lower case letters asequivalent to their upper case counterparts, so that, for example, itconsiders @samp{Funky Stuff}, @samp{funky STUFF}, and @samp{fUNKystuFf} to all be the same. To request this, use the @option{-i} or@option{--ignore-case} option.@node Brief@section Summarizing Which Files Differ@cindex summarizing which files differ@cindex brief difference reportsWhen you only want to find out whether files are different, and youdon't care what the differences are, you can use the summary outputformat. In this format, instead of showing the differences between thefiles, @command{diff} simply reports whether files differ. The @option{-q}or @option{--brief} option selects this output format.This format is especially useful when comparing the contents of twodirectories. It is also much faster than doing the normal line by linecomparisons, because @command{diff} can stop analyzing the files as soon asit knows that there are any differences.You can also get a brief indication of whether two files differ by using@command{cmp}. For files that are identical, @command{cmp} produces nooutput. When the files differ, by default, @command{cmp} outputs the byteand line number where the first difference occurs, or reports that onefile is a prefix of the other. You can usethe @option{-s}, @option{--quiet}, or @option{--silent} option tosuppress that information, so that @command{cmp}produces no output and reports whether the files differ using only itsexit status (@pxref{Invoking cmp}).@c Fix this.Unlike @command{diff}, @command{cmp} cannot compare directories; it can onlycompare two files.@node Binary@section Binary Files and Forcing Text Comparisons@cindex binary file diff@cindex text versus binary diffIf @command{diff} thinks that either of the two files it is comparing isbinary (a non-text file), it normally treats that pair of files much asif the summary output format had been selected (@pxref{Brief}), andreports only that the binary files are different. This is because lineby line comparisons are usually not meaningful for binary files.@command{diff} determines whether a file is text or binary by checking thefirst few bytes in the file; the exact number of bytes is systemdependent, but it is typically several thousand. If every byte inthat part of the file is non-null, @command{diff} considers the file to betext; otherwise it considers the file to be binary.Sometimes you might want to force @command{diff} to consider files to betext. For example, you might be comparing text files that containnull characters; @command{diff} would erroneously decide that those arenon-text files. Or you might be comparing documents that are in aformat used by a word processing system that uses null characters toindicate special formatting. You can force @command{diff} to consider allfiles to be text files, and compare them line by line, by using the@option{-a} or @option{--text} option. If the files you compare using thisoption do not in fact contain text, they will probably contain fewnewline characters, and the @command{diff} output will consist of hunksshowing differences between long lines of whatever characters the filescontain.You can also force @command{diff} to report only whether files differ(but not how). Use the @option{-q} or @option{--brief} option forthis.Normally, differing binary files count as trouble because theresulting @command{diff} output does not capture all the differences.This trouble causes @command{diff} to exit with status 2. However,this trouble cannot occur with the @option{-a} or @option{--text}option, or with the @option{-q} or @option{--brief} option, as theseoptions both cause @command{diff} to generate a form of output thatrepresents differences as requested.In operating systems that distinguish between text and binary files,@command{diff} normally reads and writes all data as text. Use the@option{--binary} option to force @command{diff} to read and write binarydata instead. This option has no effect on a @acronym{POSIX}-compliant systemlike @acronym{GNU} or traditional Unix. However, many personal computeroperating systems represent the end of a line with a carriage returnfollowed by a newline. On such systems, @command{diff} normally ignoresthese carriage returns on input and generates them at the end of eachoutput line, but with the @option{--binary} option @command{diff} treatseach carriage return as just another input character, and does notgenerate a carriage return at the end of each output line. This can beuseful when dealing with non-text files that are meant to beinterchanged with @acronym{POSIX}-compliant systems.The @option{--strip-trailing-cr} causes @command{diff} to treat inputlines that end in carriage return followed by newline as if they endin plain newline. This can be useful when comparing text that isimperfectly imported from many personal computer operating systems.This option affects how lines are read, which in turn affects how theyare compared and output.If you want to compare two files byte by byte, you can use the@command{cmp} program with the @option{-l} or @option{--verbose}option to show the values of each differing byte in the two files.With @acronym{GNU} @command{cmp}, you can also use the @option{-b} or@option{--print-bytes} option to show the @acronym{ASCII} representation ofthose bytes. @xref{Invoking cmp}, for more information.If @command{diff3} thinks that any of the files it is comparing is binary(a non-text file), it normally reports an error, because suchcomparisons are usually not useful. @command{diff3} uses the same test as@command{diff} to decide whether a file is binary. As with @command{diff}, ifthe input files contain a few non-text bytes but otherwise are liketext files, you can force @command{diff3} to consider all files to be textfiles and compare them line by line by using the @option{-a} or@option{--text} option.@node Output Formats@chapter @command{diff} Output Formats@cindex output formats@cindex format of @command{diff} output@command{diff} has several mutually exclusive options for output format.The following sections describe each format, illustrating how@command{diff} reports the differences between two sample input files.@menu* Sample diff Input:: Sample @command{diff} input files for examples.* Context:: Showing differences with the surrounding text.* Side by Side:: Showing differences in two columns.* Normal:: Showing differences without surrounding text.* Scripts:: Generating scripts for other programs.* If-then-else:: Merging files with if-then-else.@end menu@node Sample diff Input@section Two Sample Input Files@cindex @command{diff} sample input@cindex sample input for @command{diff}Here are two sample files that we will use in numerous examples toillustrate the output of @command{diff} and how various options can changeit.This is the file @file{lao}:@exampleThe 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.@end exampleThis is the file @file{tzu}:@exampleThe 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!@end exampleIn this example, the first hunk contains just the first two lines of@file{lao}, the second hunk contains the fourth line of @file{lao}opposing the second and third lines of @file{tzu}, and the last hunkcontains just the last three lines of @file{tzu}.@node Context@section Showing Differences in Their Context@cindex context output format@cindex @samp{!} output formatUsually, when you are looking at the differences between files, you willalso want to see the parts of the files near the lines that differ, tohelp you understand exactly what has changed. These nearby parts of thefiles are called the @dfn{context}.@acronym{GNU} @command{diff} provides two output formats that show contextaround the differing lines: @dfn{context format} and @dfn{unifiedformat}. It can optionally show in which function or section of thefile the differing lines are found.If you are distributing new versions of files to other people in theform of @command{diff} output, you should use one of the output formatsthat show context so that they can apply the diffs even if they havemade small changes of their own to the files. @command{patch} can applythe diffs in this case by searching in the files for the lines ofcontext around the differing lines; if those lines are actually a fewlines away from where the diff says they are, @command{patch} can adjustthe line numbers accordingly and still apply the diff correctly.@xref{Imperfect}, for more information on using @command{patch} to applyimperfect diffs.@menu* Context Format:: An output format that shows surrounding lines.* Unified Format:: A more compact output format that shows context.* Sections:: Showing which sections of the files differences are in.* Alternate Names:: Showing alternate file names in context headers.@end menu@node Context Format@subsection Context FormatThe context output format shows several lines of context around thelines that differ. It is the standard format for distributing updatesto source code.To select this output format, use the @option{-C @var{lines}},@option{--context@r{[}=@var{lines}@r{]}}, or @option{-c} option. Theargument @var{lines} that some of these options take is the number oflines of context to show. If you do not specify @var{lines}, itdefaults to three. For proper operation, @command{patch} typically needsat least two lines of context.@menu* Example Context:: Sample output in context format.* Less Context:: Another sample with less context.* Detailed Context:: A detailed description of the context output format.@end menu@node Example Context@subsubsection An Example of Context FormatHere is the output of @samp{diff -c lao tzu} (@pxref{Sample diff Input},for the complete contents of the two files). Notice that up to threelines that are not different are shown around each line that isdifferent; they are the context lines. Also notice that the first twohunks have run together, because their contents overlap.@example*** lao 2002-02-21 23:30:39.942229878 -0800--- tzu 2002-02-21 23:30:50.442260588 -0800****************** 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!@end example@node Less Context@subsubsection An Example of Context Format with Less ContextHere is the output of @samp{diff -C 1 lao tzu} (@pxref{Sample diffInput}, for the complete contents of the two files). Notice that atmost one context line is reported here.@example*** lao 2002-02-21 23:30:39.942229878 -0800--- tzu 2002-02-21 23:30:50.442260588 -0800****************** 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -