Medusar's Blog
敬畏知识,谦逊前行
Toggle navigation
Medusar's Blog
主页
Booklist
Resources
About Me
归档
标签
Linux diff命令
2017-05-06 15:41:17
371
0
0
medusar
> Linux中diff命令的功能为逐行比较两个文本文件,列出其不同之处。它对给出的文件进行系统的检查,并显示出两个文件中所有不同的行,不要求事先对文件进行排序 ## 命令使用 ``` bash diff [OPTION]... FILES FILES are `FILE1 FILE2' or `DIR1 DIR2' or `DIR FILE...' or `FILE... DIR'. If --from-file or --to-file is given, there are no restrictions on FILES. If a FILE is `-', read standard input. ``` 注意: FILES参数只能为File1 File2或者 DIR1 DIR2,或者DIR FILE 或者 FILE DIR 不能有多个参数。 如果--from-file或--to-file选项在,则可以多个文件。 比如: f1,f2,f3都跟f3比较 ``` bash diff --to-file=f3 f1 f2 f3 ``` 用f3与f1,f2,f3比较 ``` bash diff --from-file=f3 f1 f2 f3 ``` 上面说的`DIR FILE...` or `FILE... DIR`, 实际上是比较目录下对应的同名文件。 比如: 比较d1目录下名为f1的文件与当前目录下f1文件,如果d1下没有,则提示文件不存在 ``` bash diff d1/ f1 ``` ``` bash -y或--side-by-side 以并列的方式显示文件的异同之处。 -W或--width 在使用-y参数时,指定栏宽。 -c 显示全部内文,并标出不同之处。 -u,-U或--unified= 以合并的方式来显示文件内容的不同。 -r或--recursive 比较子目录中的文件。 -N或--new-file 在比较目录时,若文件A仅出现在某个目录中,预设会显示:Only in目录:文件A若使用-N参数,则diff会将文件A与一个空白的文件比较。 -b或--ignore-space-change 不检查空格字符的不同 -B或--ignore-blank-lines 不检查空白行。 -H或--speed-large-files 比较大文件时,可加快速度。 -l或--ignore-matching-lines 若两个文件在某几行有所不同,而这几行同时都包含了选项中指定的字符或字符串,则不显示这两个文件的差异。 -i或--ignore-case 不检查大小写的不同。 -q或--brief 仅显示有无差异,不显示详细的信息。 -r或--recursive 比较子目录中的文件。 -s或--report-identical-files 若没有发现任何差异,仍然显示信息。 -S或--starting-file 在比较目录时,从指定的文件开始比较 -t或--expand-tabs 在输出时,将tab字符展开。 -T或--initial-tab 在每行前面加上tab字符以便对齐。 -w或--ignore-all-space 忽略全部的空格字符。 -v或--version 显示版本信息。 ``` ## 常用命令举例 ### 常用选项 1. -u 2. -N 3. -c 4. -e 需要看懂输出 5. -y 分列输出 ### 普通比较 ``` bash diff f1 f2 ``` 普通比较的输出结果解读  ``` bash 1a2 > world ``` 上面结果表示,第二个文件的第二行是在第一个文件的第一行后面增加的。 ### 上下文输出(Context Format) 上下文输出的作用是为了方便查看不同,除了输出不同之处之外,还会输出对应的前后几行。 ``` bash diff -c f1 f2 ``` 输出格式说明 ``` bash Detailed Description of Context Format ...................................... 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 The time stamp normally looks like `2002-02-21 23:30:39.942229878 -0800' to indicate the date, time with fractional seconds, and time zone in Internet RFC 2822 format (ftp://ftp.isi.edu/in-notes/rfc2822.txt). However, a traditional time stamp like `Thu Feb 21 23:30:39 2002' is used if the `LC_TIME' locale category is either `C' or `POSIX'. You can change the header's content with the `--label=LABEL' option; see *Note Alternate 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: `!' A line that is part of a group of one or more lines that changed between the two files. There is a corresponding group of lines marked with `!' in the part of this hunk for the other file. `+' An "inserted" line in the second file that corresponds to nothing in the first file. `-' A "deleted" line in the first file that corresponds to nothing in the second file. 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. ``` ### 一体化输出(Unified Format) 是上下文输出的一个变种。 ``` bash `-U LINES', `--unified[=LINES]', or `-u' option。 LINES是指定输出的上下文行数,默认3行。 ``` 输出格式: ``` bash Detailed Description of Unified Format ...................................... 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 The time stamp looks like `2002-02-21 23:30:39.942229878 -0800' to indicate the date, time with fractional seconds, and time zone. You can change the header's content with the `--label=LABEL' option; see *Note Alternate 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 print column: `+' A line was added here to the first file. `-' A line was removed here from the first file. ``` ### 并排比较 ``` bash diff -y f1 f2 ``` 输出说明 ``` bash `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: white space The corresponding lines are in common. That is, either the lines are identical, or the difference is ignored because of one of the `--ignore' options (*note White Space::). `|' The corresponding lines differ, and they are either both complete or both incomplete. `<' The files differ and only the first file contains the line. `>' The files differ and only the second file contains the line. `(' Only the first file contains the line, but the difference is ignored. `)' Only the second file contains the line, but the difference is ignored. `\' The corresponding lines differ, and only the first line is incomplete. `/' The corresponding lines differ, and only the second line is incomplete. ``` ### 生成patch文件 ``` bash diff -urN old/ new/ > mysoft.patch 然后 patch -p0 < mysoft.patch ##参考(info diff中有,patch命令) ``` ## 比较3个文件 ``` bash diff3 f1 f2 f3 ``` 输出解释: ``` bash File: diff.info, Node: Detailed diff3 Normal, Next: diff3 Hunks, Prev: Sample diff3 Input, Up: C\ omparing Three Files 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. *Note Tabs::, 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 (*note Incomplete Lines::), it is distinguished on output from a full line by a following line that starts with `\'. ``` 比如: ``` bash BJ:practice hell$ diff3 lao tzu tao ====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 ``` ### 并排比较并merge ``` bash sdiff -o outputfile f1 f2 ``` 该命令会以交互的形式让你选择合并内容,并把结果输出到outputfile中。交互命令说明: ``` bash File: diff.info, Node: Merge Commands, Prev: sdiff Option Summary, Up: Interactive Merging Merge Commands ============== Groups of common lines, with a blank gutter, are copied from the first file to the output. After each group of differing lines, `sdiff' prompts with `%' and pauses, waiting for one of the following commands. Follow each command with <RET>. `e' Discard both versions. Invoke a text editor on an empty temporary file, then copy the resulting file to the output. `eb' Concatenate the two versions, edit the result in a temporary file, then copy the edited result to the output. `ed' Like `eb', except precede each version with a header that shows what file and lines the version came from. `el' Edit a copy of the left version, then copy the result to the output. `er' Edit a copy of the right version, then copy the result to the output. `l' Copy the left version to the output. `q' Quit. `r' Copy the right version to the output. `s' Silently copy common lines. `v' Verbosely copy common lines. This is the default. The text editor invoked is specified by the `EDITOR' environment variable if it is set. The default is system-dependent. ```
上一篇:
有用的Shell片段整理
下一篇:
Grep AND OR NOT
0
赞
371 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus
文档导航