diff和common

diff 命令

diff命令:找出两个文件的不同点,用于比较文件的差异

linux上非常重要的工具,一般用于制作补丁文件,特别是比较两个版本不同的文件以找到改动的地方。

diff在命令行中打印每一个行的改动。最新版本的diff还支持二进制文件。diff程序的输出被称为补丁 (patch),因为Linux系统中还有一个patch程序,可以根据diff的输出将a.c的文件内容更新为b.c。diff是svn、cvs、git等版本控制工具不可或缺的一部分。

 

最简单的情况是, diff 比较两个文件的内容 (源文件目标文件)。 作为特别的情况是, diff - - 比较一份标准输入的它自己的拷贝如果 源文件 是一个目录和 目标文件 不是(目录), diff 会比较在 源文件(目录) 里的文件的中和 目标文件同名的(文件), 反过来也一样. 非目录文件不能是 -. 如果 源文件目标文件 都是目录, diff 比较两个目录中相应的文件,依照字母次序排序;这个比较是不会递归的,除非给出 -r 或者 --recursive. diff 不把一个目录的内容看为它是一个文件来比较。被指定的文件不能是标准的输入, 因为标准的输入是无名的并且"有一样的名字的文件"的观点不适用。 diff 的选项由 -, 开始所以正常地 源文件(名)目标文件(名) 不可以用 - 开头. 然而, -- 可以被它视为保留的即使作为文件名的开头

diff命令能比较单个文件或者目录内容。如果指定比较的是文件,则只有当输入为文本文件时才有效。以逐行的方式,比较文本文件的异同处。如果指定比较的是目录的的时候,diff 命令会比较两个目录下名字相同的文本文件。列出不同的二进制文件、公共子目录和只在一个目录出现的文件。

 

选项

下面是 GNU所接受的 diff 的所有选项的概要. 大多数的选项有两个相同的名字,一个是单个的跟在 - 后面字母, 另一个是由 -- 引出的长名字. 多个单字母选项(除非它们产生歧义)能够组合为单行的命令行语法 -ac 是等同于 -a -c. 长命名的选项能被缩短到他们的名字的任何唯一的前缀. 用 ([]) 括起来显示选项产生歧义的选项

-行数(一个整数)
显示上下文 行数 (一个整数). 这个选项自身没有指定输出格式,这是没有效果的,除非和 -c 或者 -u 组合使用. 这是已废置的选项,对于正确的操作, 上下文至少要有两行。
-a
所有的文件都视为文本文件来逐行比较,甚至他们似乎不是文本文件.
-b
忽略空格引起的变化.
-B
忽略插入删除空行引起的变化.
--brief
仅报告文件是否相异,在乎差别的细节.
-c
使用上下文输出格式.
-C 行数(一个整数)
--context[=lines]
使用上下文输出格式,显示以指定 行数 (一个整数), 或者是三行(当 行数 没有给出时. 对于正确的操作, 上下文至少要有两行.
--changed-group-format=format
使用 format 输出一组包含两个文件的不同处的行,其格式是 if-then-else .
-d
改变算法也许发现变化的一个更小的集合.这会使 diff 变慢 (有时更慢).
-D name
合并 if-then-else 格式输出, 预处理宏(由name参数提供)条件.
-e
--ed
输出为一个有效的 ed 脚本.
--exclude=pattern
比较目录的时候,忽略和目录中与 pattern(样式) 相配的.
--exclude-from=file
比较目录的时候,忽略和目录中与任何包含在 file(文件) 的样式相配的文件和目录.
--expand-tabs
在输出时扩展tab为空格,保护输入文件的tab对齐方式
-f
产生一个很象 ed 脚本的输出,但是但是在他们在文件出现的顺序有改变
-F regexp
在上下文和统一格式中,对于每一大块的不同,显示出匹配 regexp. 的一些前面的行.
--forward-ed
产生象 ed 脚本的输出,但是它们在文件出现的顺序有改变。
-h
这选项现在已没作用,它呈现Unix的兼容性.
-H
使用启发规则加速操作那些有许多离散的小差异的大文件.
--horizon-lines=lines
比较给定行数的有共同前缀的最后行,和有共同或缀的最前行.
-i
忽略大小写.
-I regexp
忽略由插入,删除行(由regexp 参数提供参考)带来的改变.
--ifdef=name
合并 if-then-else 格式输出, 预处理宏(由name参数提供)条件.
--ignore-all-space
在比较行的时候忽略空白.
--ignore-blank-lines
忽略插入和删除空行
--ignore-case
忽略大小写.
--ignore-matching-lines=regexp
忽略插入删除行(由regexp 参数提供参考).
--ignore-space-change
忽略空白的数量.
--initial-tab
在文本行(无论是常规的或者格式化的前后文关系)前输出tab代替空格. 引起的原因是tab对齐方式看上去象是常规的一样.
-l
产生通过 pr 编码的输出.
-L label
--label=label
使用 label 给出的字符在文件头代替文件名输出.
--left-column
以并列方式印出两公共行的左边
--line-format=format
使用 format 输出所有的行,在 if-then-else 格式中.
--minimal
改变算法也许发现变化的一个更小的集合.这会使 diff 变慢 (有时更慢).
-n
输出 RC-格式 diffs; 除了每条指令指定的行数受影响外 象 -f 一样。
-N
--new-file
在目录比较中,如果那个文件只在其中的一个目录中找到,那么它被视为在另一个目录中是一个空文件.
--new-group-format=format
使用 format 以if-then-else 格式输出只在第二个文件中取出的一个行组
--new-line-format=format
使用 format 以if-then-else 格式输出只在第二个文件中取出的一行
--old-group-format=format
使用 format 以if-then-else 格式输出只在第一个文件中取出的一个行组
--old-line-format=format
使用 format 使用 format 以if-then-else 格式输出只在第一个文件中取出的一行
-p
显示带有c函数的改变.
-P
在目录比较中,如果那个文件只在其中的一个目录中找到,那么它被视为在另一个目录中是一个空文件.
--paginate
产生通过 pr 编码的输出.
-q
仅报告文件是否相异,不报告详细的差异.
-r
当比较目录时,递归比较任何找到的子目录.
--rcs
输出 RC-格式 diffs; 除了每条指令指定的行数受影响外 象 -f 一样。
--recursive
当比较目录时,递归比较任何找到的子目录.
--report-identical-files
-s
报告两个文件相同.
-S file
当比较目录时,由 file 开始. 这用于继续中断了的比较.
--sdiff-merge-assist
打印附加的信息去帮助 sdiff. sdiff 在运行 diff 时使用这些选项. 这些选项不是特意为使用者直接使用而准备的。
--show-c-function
显示带有c函数的改变.
--show-function-line=regexp
在上下文和统一的格式,对于每一大块的差别,显示出匹配 regexp. 的一些前面的行
--side-by-side
使用并列的输出格式.
--speed-large-files
使用启发规则加速操作那些有许多离散的小差异的大文件.
--starting-file=file
当比较目录时,由 file 开始. 这用于继续中断了的比较.
--suppress-common-lines
在并列格式中不印出公共行。
-t
在输出时扩展tab为空格,保护输入文件的tab对齐方式
-T
在文本行(无论是常规的或者格式化的前后文关系)前输出tab代替空格.引起的原因是tab对齐方式看上去象是常规的一样.
--text
所有的文件都视为文本文件来逐行比较,甚至他们似乎不是文本文件.
-u
使用统一的输出格式.
--unchanged-group-format=format
使用 format 输出两个文件的公共行组,其格式是if-then-else.
--unchanged-line-format=format
使用 format 输出两个文件的公共行,其格式是if-then-else.
--unidirectional-new-file
在目录比较中,如果那个文件只在其中的一个目录中找到,那么它被视为在另一个目录中是一个空文件.
-U lines
--unified[=lines]
使用前后关系格式输出,显示以指定 行数 (一个整数), 或者是三行(当 行数 没有给出时. 对于正确的操作, 上下文至少要有两行.
-v
--version
输出 diff 版本号.
-w
在比较行时忽略空格
-W columns
--width=columns
在并列格式输出时,使用指定的列宽.
-x pattern
比较目录的时候,忽略和目录中与 pattern(样式) 相配的.
-X file
比较目录的时候,忽略和目录中与任何包含在 file(文件) 的样式相配的文件和目录.
-y
使用并列格式输出 

 

通过实例看懂diff命令输出 
###############################

实例: 有这样两个文件: 程序清单1 :hello.c #include <stdio.h> int main(void) { char msg[] = "Hello world!"; puts(msg); printf("Welcome to use diff commond.\n"); return 0; }

###############################

程序清单2:hello_diff.c #include <stdio.h> #include <stdlib.h> int main(void) { char msg[] = "Hello world,fome hello_diff.c"; puts(msg); printf("hello_diff.c says,'Here you are,using diff.'\n"); return 0; }
我们使用diff命令来查看这两个文件的不同之处,有一下几种方便的方法:
1、普通格式输出: [root@localhost diff]# diff hello.c hello_diff.c 1a2 > #include <stdlib.h> 5c6 < char msg[] = "Hello world!"; --- > char msg[] = "Hello world,fome hello_diff.c"; 8c9 < printf("Welcome to use diff commond.\n"); --- > printf("hello_diff.c says,'Here you are,using diff.'\n"); [root@localhost diff]# 上面的“1a2”表示后面的一个文件"hello_diff.c"比前面的一个文件"hello.c"多了一行 "5c6"表示第一个文件的第5行与第二个文件的第6行有区别 2、并排格式输出 [root@localhost diff]#
diff -y -W 130 hello.c hello_diff.c
#include <stdio.h> #include <stdio.h> > #include <stdlib.h> int main(void) int main(void) { { char msg[] = "Hello world!"; | char msg[] = "Hello world,fome hello_diff.c"; puts(msg); puts(msg); printf("Welcome to use diff commond.\n"); | printf("hello_diff.c says,'Here you are,using diff.'\ return 0; return 0; } } [root@localhost diff]# 这种并排格式的对比一目了然,可以快速找到不同的地方。 -W选择可以指定输出列的宽度,这里指定输出列宽为130 3、上下文输出格式 [root@localhost diff]# diff hello.c hello_diff.c -c *** hello.c 2007-09-25 17:54:51.000000000 +0800 --- hello_diff.c 2007-09-25 17:56:00.000000000 +0800 *************** *** 1,11 **** #include <stdio.h> int main(void) { ! char msg[] = "Hello world!"; puts(msg); ! printf("Welcome to use diff commond.\n"); return 0; } --- 1,12 ---- #include <stdio.h> + #include <stdlib.h> int main(void) { ! char msg[] = "Hello world,fome hello_diff.c"; puts(msg); ! printf("hello_diff.c says,'Here you are,using diff.'\n"); return 0; } [root@localhost diff]# 这种方式在开头两行作了比较文件的说明,这里有三中特殊字符: + 比较的文件的后者比前着多一行 - 比较的文件的后者比前着少一行 ! 比较的文件两者有差别的行 4、统一输出格式 [root@localhost diff]# diff hello.c hello_diff.c -u --- hello.c 2007-09-25 17:54:51.000000000 +0800 +++ hello_diff.c 2007-09-25 17:56:00.000000000 +0800 @@ -1,11 +1,12 @@ #include <stdio.h> +#include <stdlib.h> int main(void) { - char msg[] = "Hello world!"; + char msg[] = "Hello world,fome hello_diff.c"; puts(msg); - printf("Welcome to use diff commond.\n"); + printf("hello_diff.c says,'Here you are,using diff.'\n"); return 0; } [root@localhost diff]# 正如看到的那样,统一格式的输出更加紧凑,所以更易于理解,更易于修改。 5、其他 假如你想查看两个文件是否不同又不想显示差异之处的话,可以加上-q选项: [root@localhost diff]# diff hello.c hello_diff.c -q Files hello.c and hello_diff.c differ [root@localhost diff]# 另外你还可以提供一些匹配规则来忽略某中差别,可以用 -I regexp [root@localhost diff]# diff hello.c hello_diff.c -c -I include *** hello.c 2007-09-25 17:54:51.000000000 +0800 --- hello_diff.c 2007-09-25 17:56:00.000000000 +0800 *************** *** 2,11 **** int main(void) { ! char msg[] = "Hello world!"; puts(msg); ! printf("Welcome to use diff commond.\n"); return 0; } --- 3,12 ---- int main(void) { ! char msg[] = "Hello world,fome hello_diff.c"; puts(msg); ! printf("hello_diff.c says,'Here you are,using diff.'\n"); return 0; } [root@localhost diff]# 这里通过“ -I include”选项来忽略带有“ include”字样的行

 

4.使用实例:

 

实例1:比较两个文件

命令:

输出:

[root@localhost test3]diff log2014.log log2013.log 

3c3

2014-03

---

> 2013-03

8c8

2013-07

---

> 2013-08

11,12d10

2013-11

2013-12

 

说明:

上面的“3c3”和“8c8”表示log2014.log和log20143log文件在3行和第8行内容有所不同;"11,12d10"表示第一个文件比第二个文件多了第11和12行。

diff normal 显示格式有三种提示:

a - add

c - change

d - delete 

 

实例2:并排格式输出

命令:

diff log2013.log log2014.log  -y -W 50

输出:

[root@localhost test3]diff log2014.log log2013.log  -y -W 50

2013-01                 2013-01

2013-02                 2013-02

2014-03               | 2013-03

2013-04                 2013-04

2013-05                 2013-05

2013-06                 2013-06

2013-07                 2013-07

2013-07               | 2013-08

2013-09                 2013-09

2013-10                 2013-10

2013-11               <

2013-12               <

[root@localhost test3]diff log2013.log log2014.log  -y -W 50

2013-01                 2013-01

2013-02                 2013-02

2013-03               | 2014-03

2013-04                 2013-04

2013-05                 2013-05

2013-06                 2013-06

2013-07                 2013-07

2013-08               | 2013-07

2013-09                 2013-09

2013-10                 2013-10

                      > 2013-11

                      > 2013-12

 

说明:

“|”表示前后2个文件内容有不同

<”表示后面文件比前面文件少了1行内容

>”表示后面文件比前面文件多了1行内容

 

实例3:上下文输出格式

命令:

diff log2013.log log2014.log  -c

输出:

[root@localhost test3]diff log2013.log log2014.log  -c

*** log2013.log 2012-12-07 16:36:26.000000000 +0800

--- log2014.log 2012-12-07 18:01:54.000000000 +0800

***************

*** 1,10 ****

  2013-01

  2013-02

! 2013-03

  2013-04

  2013-05

  2013-06

  2013-07

! 2013-08

  2013-09

  2013-10

--- 1,12 ----

  2013-01

  2013-02

! 2014-03

  2013-04

  2013-05

  2013-06

  2013-07

! 2013-07

  2013-09

  2013-10

+ 2013-11

+ 2013-12[root@localhost test3]diff log2014.log log2013.log  -c

*** log2014.log 2012-12-07 18:01:54.000000000 +0800

--- log2013.log 2012-12-07 16:36:26.000000000 +0800

***************

*** 1,12 ****

  2013-01

  2013-02

! 2014-03

  2013-04

  2013-05

  2013-06

  2013-07

! 2013-07

  2013-09

  2013-10

- 2013-11

- 2013-12

--- 1,10 ----

  2013-01

  2013-02

! 2013-03

  2013-04

  2013-05

  2013-06

  2013-07

! 2013-08

  2013-09

  2013-10[root@localhost test3]#

 

说明:

这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:

“+” 比较的文件的后者比前着多一行

“-” 比较的文件的后者比前着少一行

“!” 比较的文件两者有差别的行

 

实例4:统一格式输出

命令:

diff log2014.log log2013.log  -u

输出:

[root@localhost test3]diff log2014.log log2013.log  -u

--- log2014.log 2012-12-07 18:01:54.000000000 +0800

+++ log2013.log 2012-12-07 16:36:26.000000000 +0800

@@ -1,12 +1,10 @@

 2013-01

 2013-02

-2014-03

+2013-03

 2013-04

 2013-05

 2013-06

 2013-07

-2013-07

+2013-08

 2013-09

 2013-10

-2013-11

-2013-12

 

说明:

它的第一部分,也是文件的基本信息:

--- log2014.log 2012-12-07 18:01:54.000000000 +0800

+++ log2013.log 2012-12-07 16:36:26.000000000 +0800

"---"表示变动前的文件,"+++"表示变动后的文件。

第二部分,变动的位置用两个@作为起首和结束。

   @@ -1,12 +1,10 @@

前面的"-1,12"分成三个部分:减号表示第一个文件(即log2014.log),"1"表示第1行,"12"表示连续12行。合在一起,就表示下面是第一个文件从第1行开始的连续12行。同样的,"+1,10"表示变动后,成为第二个文件从第1行开始的连续10行。

 

实例5:比较文件夹不同

命令:

diff  test3 test6

输出:

[root@localhost test]# diff test3 test6

Only in test6: linklog.log

Only in test6: log2012.log

diff test3/log2013.log test6/log2013.log

1,10c1,3

< 2013-01

< 2013-02

< 2013-03

< 2013-04

< 2013-05

< 2013-06

< 2013-07

< 2013-08

< 2013-09

< 2013-10

---

> hostnamebaidu=baidu.com

> hostnamesina=sina.com

> hostnames=true

diff test3/log2014.log test6/log2014.log

1,12d0

< 2013-01

< 2013-02

< 2014-03

< 2013-04

< 2013-05

< 2013-06

< 2013-07

< 2013-07

< 2013-09

< 2013-10

< 2013-11

< 2013-12

Only in test6: log2015.log

Only in test6: log2016.log

Only in test6: log2017.log

[root@localhost test]# 

说明:

 

实例6:比较两个文件不同,并生产补丁

命令:

diff -ruN log2013.log log2014.log >patch.log

输出:

[root@localhost test3]diff -ruN log2013.log log2014.log >patch.log

[root@localhost test3]ll

总计 12

-rw-r--r-- 2 root root  80 12-07 16:36 log2013.log

-rw-r--r-- 1 root root  96 12-07 18:01 log2014.log

-rw-r--r-- 1 root root 248 12-07 21:33 patch.log

[root@localhost test3]cat patc.log

cat: patc.log: 没有那个文件或目录

[root@localhost test3]cat patch.log 

--- log2013.log 2012-12-07 16:36:26.000000000 +0800

+++ log2014.log 2012-12-07 18:01:54.000000000 +0800

@@ -1,10 +1,12 @@

 2013-01

 2013-02

-2013-03

+2014-03

 2013-04

 2013-05

 2013-06

 2013-07

-2013-08

+2013-07

 2013-09

 2013-10

+2013-11

+2013-12[root@localhost test3]#

说明:

 

实例7:打补丁

命令:

输出:

[root@localhost test3]cat log2013.log

2013-01

2013-02

2013-03

2013-04

2013-05

2013-06

2013-07

2013-08

2013-09

2013-10[root@localhost test3]patch log2013.log patch.log 

patching file log2013.log

[root@localhost test3]

[root@localhost test3]cat log2013.log 

2013-01

2013-02

2014-03

2013-04

2013-05

2013-06

2013-07

2013-07

2013-09

2013-10

2013-11

2013-12[root@localhost test3]#

说明:

 

 

 

 

 

1.diff格式参数
-u 输出统一格式,-c是传统格式。diff有"传统"和"统一"两种格式,一般使用"统一"格式,即-u . 比较而言,统一格式生成的文件大,但包含了更多的信息,有利于阅读与定位
注意到-c 与-u这二种格式不能同时使用。你只能使用其中一种格式化输出内容
diff -u -c   /home/gaoyibo/php-site/php-site/src/Search/web.xml /home/gaoyibo/search/workspace/searchServer/WebContent/WEB-INF/web.xml > web.patch
diff: conflicting output style options
diff: Try `diff --help' for more information.
 
了解-u的输出格式:同一块内容,里面用+,-区分文件的修改。
--- /home/gaoyibo/php-site/php-site/src/Search/web.xml    2011-11-08 15:38:07.000000000 +0800
+++ /home/gaoyibo/search/workspace/searchServer/WebContent/WEB-INF/web.xml    2011-11-07 11:22:45.000000000 +0800
@@ -25,8 +25,9 @@
     <servlet-name>InitServlet</servlet-name>
     <servlet-class>com.daodao.servlet.InitServlet</servlet-class>
     <init-param>
+      <!-- change path -->
       <param-name>propspath</param-name>
-      <param-value>/home/search/config/</param-value>
+      <param-value>/home/gaoyibo/search/resource/</param-value>
     </init-param>
     <init-param>
       <param-name>propsname</param-name>
2.diff目录用到参数
如果比较二个目录,使用-r,表示Recursively compare any subdirectories found.      -x 用来排除目录中的某个文件
 
 

**3.其它参数
-a  Treat all files as text.
-b  Ignore changes in the amount of white space.
-N   如果某个文件只在一个目录中出现,则假定在另一个目录中为空文件.

查看diff -acb生成的patch文件:

示例1:注意下页的示例只为说明-c,推荐使用-u。

不会针对同行做修改。只有增减。注意,这种情况下,---文件的变化都是在***里使用“-”来标志。---部分没有内容。这与!时的情况不同。

diff -r -a -c -b -x Makefile.temp /home/gaoyibo/search/workspace/searchServer/src/com/daodao/application/search/Indexer.java /home/gaoyibo/php-site/php-site/src/Search/src/com/daodao/application/search/Indexer.java
*** /home/gaoyibo/search/workspace/searchServer/src/com/daodao/application/search/Indexer.java    2011-10-27 14:21:12.000000000 +0800
--- /home/gaoyibo/php-site/php-site/src/Search/src/com/daodao/application/search/Indexer.java    2011-11-08 15:38:07.000000000 +0800
***************
*** 1,7 ****
  package com.daodao.application.search;
  
  import java.io.BufferedOutputStream;
- 
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileInputStream;
--- 1,6 ----
***************
*** 62,68 ****
  //    private static PinYinSearchTree m_cPinYinSearchForLocation = new PinYinSearchTree();
     
  
-     //
      public static LinkedBlockingQueue<Document> m_lDocQueue = new LinkedBlockingQueue<Document>(200000);
      
      
--- 61,66 ----
***************
*** 106,112 ****
          }
      }
  
-     
      private Indexer(Properties prop)
      {
          _init(prop);
--- 104,109 ----
***************
*** 120,126 ****
              {
                  prop = DaoDaoConfig.getProperties();
              }
-             //私有的构造方法供单例使用。
              m_iInstance = new Indexer(prop);             
          }
          return m_iInstance;
--- 117,122 ----

 

 

示例2:对同行做修改。注意---部分格式的变化。

diff -r -a -c -b -x Makefile.temp /home/gaoyibo/search/workspace/searchServer/src/com/daodao/servlet/Init.java /home/gaoyibo/php-site/php-site/src/Search/src/com/daodao/servlet/Init.java
*** /home/gaoyibo/search/workspace/searchServer/src/com/daodao/servlet/Init.jav2011-11-08 19:51:56.000000000 +0800
--- /home/gaoyibo/php-site/php-site/src/Search/src/com/daodao/servlet/Init.java2011-11-08 19:51:46.000000000 +0800
***************
*** 32,38 ****
          // DaoDaoPostDBReader.getInstance();
          // get location related info
          long lStart = System.currentTimeMillis();
!         // DaoDaoLocationReader.getInstance();
          long lSpan = System.currentTimeMillis() - lStart;
          lStart = System.currentTimeMillis();
          DaoDaoLogging.SERVLET.info("Loading all location info takes " + lSpan
--- 32,38 ----
          // DaoDaoPostDBReader.getInstance();
          // get location related info
          long lStart = System.currentTimeMillis();
!         DaoDaoLocationReader.getInstance();
          long lSpan = System.currentTimeMillis() - lStart;
          lStart = System.currentTimeMillis();
          DaoDaoLogging.SERVLET.info("Loading all location info takes " + lSpan
***************
*** 48,54 ****
  
          if (DaoDaoConfig.getStringProperty(GEOPREFIXSEARCH).equalsIgnoreCase(
                  "true")) {
! //            GeoPrefixSearch.getInstance();
              lSpan = System.currentTimeMillis() - lStart;
              lStart = System.currentTimeMillis();
              DaoDaoLogging.SERVLET.info("Loading all geo prefix index takes "
--- 48,54 ----
  
          if (DaoDaoConfig.getStringProperty(GEOPREFIXSEARCH).equalsIgnoreCase(
                  "true")) {
!             GeoPrefixSearch.getInstance();
              lSpan = System.currentTimeMillis() - lStart;
              lStart = System.currentTimeMillis();
              DaoDaoLogging.SERVLET.info("Loading all geo prefix index takes 



 

 

 

 

 

 

 

 

Unix/Linux文件比较命令: comm命令, Diff命令 



Comm命令


如果想对两个有序的文件进行比较,可以使用comm命令。 

语法:comm [- 123 ] file1 file2 
 
说明:该命令是对两个已经排好序的文件进行比较。其中file1和file2是已排序的文件。comm读取这两个文件,然后生成三列输出:仅在file1中出现的行;仅在file2中出现的行;在两个文件中都存在的行。如果文件名用“- ”,则表示从标准输入读取。 

选项1、2或3抑制相应的列显示。例如
comm - 12就只显示在两个文件中都存在的行;
comm - 23只显示在第一个文件中出现而未在第二个文件中出现的行;
comm - 123则什么也不显示。 

例如:假设要对文件myfile1和myfile2进行比较 

$ cat myfile1 

main( ) 

{ 

float a,b, i, j ,z ; 

a=i=10 ; b=j=5 ; 

z= i + j ; 

printf(“z=%d\\\\n”,z) ; 

} 

$ cat myfile2 

#include 

main( ) 

{ 

float i, j ,z ; 

i=10 ; j=5 ; 

z= i + j ; 

printf(“z=%f\\\\n”,z) ; 

} 

$ comm - 12 myfile1 myfile2 

main( ) 

{ 

z= i + j ; 

} 

就只显示文件myfile1和myfile2中共有的行。 

----------------------------------------------------
Diff命令 

该命令的功能为逐行比较两个文本文件,列出其不同之处。它比comm命令完成更复杂的检查。它对给出的文件进行系统的检查,并显示出两个文件中所有不同的行,不要求事先对文件进行排序。 

语法:diff [选项] file1 file2 

说明:该命令告诉用户,为了使两个文件file1和file2一致,需要修改它们的哪些行。如果用“- ”表示file1或fiie2,则表示标准输入。如果file1或file2是目录,那么diff将使用该目录中的同名文件进行比较。例如: 

diff /usr/xu mine 

把目录/usr/xu 中名为mine的文件与当前目录中的mine文件进行比较。 

通常输出由下述形式的行组成: 

n1 a n3,n4 

n1,n2 d n3 

n1,n2 c n3,n4 

这些行类似ed命令把filel转换成file2。字母(a、d和c)之前的行号(n1,n2)是针对file1的,其后面的行号(n3,n4)是针对file2的。字母a、d和c分别表示附加、删除和修改操作。 

在上述形式的每一行的后面跟随受到影响的若干行,以“<”打头的行属于第一个文件,以“>”打头的行属于第二个文件。 

diff能区别块和字符设备文件以及FIFO(管道文件),不会把它们与普通文件进行比较。 

如果file1和file2都是目录,则diff会产生很多信息。如果一个目录中只有一个文件,则产生一条信息,指出该目录路径名和其中的文件名。 

diff各选项的含义如下: 

- b 忽略行尾的空格,而字符串中的一个或多个空格符都视为相等。如How are you与How are you被视为相同的字符串。 

- c 采用上下文输出格式(提供三行上下文)。 

- C n 采用上下文输出格式(提供n行上下文)。 

- e 产生一个合法的ed脚本作为输出。 

- r 当file1和file2是目录时,递归作用到各文件和目录上。 

例如,文件ml.c的内容为(左边行号是有意加上的,以便前后对照): 

1 main( ) 

{ 

printf(“Hello!\n”); 

} 

5 

文件m2.c的内容为: 

1 main() 

2 { 

3 int n , m ; 

4 n= 10 ; 

5 printf ( “ % d \\\\ n ” , m = n * 10); 

6 } 

输入命令: 

$ diff m1.c m2.c 

屏幕上显示: 

3,5 c 3,6 

printf(“Hello!\n”); 

} 

<5 

>3 int n,m; 

>4 n=10 ; 

>5 printf ( “ % d \\\\ n ” , m = n * 10); 

>6 } 

 表示把文件m1.c的3至5行改成m2.c的3至6行后,两个文件相同。 

 

 

posted @ 2014-07-02 22:34  Agoly  阅读(643)  评论(0编辑  收藏  举报