摘自:http://blog.sina.com.cn/s/blog_612144f30100nkpt.html

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

实例:

有这样两个文件:
程序清单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 hello.c hello_diff.c -y -W 130
#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”字样的行

通过实例看懂diff命令输出的更多相关文章

  1. 每天一个命令day1【diff 命令】(具体实例看下一节)

    diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...

  2. diff命令的参数详解和实例 【转】

    转自:http://blog.chinaunix.net/uid-25324849-id-270254.html diff命令参数: diff - 找出两个文件的不同点 总览 diff [选项] 源文 ...

  3. diff命令的参数详解和实例

    diff命令参数: diff - 找出两个文件的不同点 总览 diff [选项] 源文件 目标文件 描述 在最简单的情况是, diff 比较两个文件的内容 (源文件 和 目标文件). 文件名可以是 - ...

  4. diff命令

    描述 在最简单的情况是, diff 比较两个文件的内容 (源文件 和 目标文件). 文件名可以是 - 由标准输入设备读入的文本. 作为特别的情况是, diff - - 比较一份标准输入的它自己的拷贝如 ...

  5. diff命令参数

    diff命令参数: diff - 找出两个文件的不同点 总览 diff [选项] 源文件 目标文件 描述 在最简单的情况是, diff 比较两个文件的内容 (源文件 和 目标文件). 文件名可以是 - ...

  6. 每天一个linux命令(49)--diff命令

    diff 命令是 Linux 上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff 在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件,diff ...

  7. 追源索骥:透过源码看懂Flink核心框架的执行流程

    li,ol.inline>li{display:inline-block;padding-right:5px;padding-left:5px}dl{margin-bottom:20px}dt, ...

  8. linux shell 脚本攻略学习13--file命令详解,diff命令详解

    一.file命令详解 find命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从 ...

  9. diff命令具体解释

    diff命令參数: diff - 找出两个文件的不同点 总览 diff [选项] 源文件 目标文件 以下是 GNU所接受的 diff 的全部选项的概要. 大多数的选项有两个同样的名字,一个是单个的跟在 ...

随机推荐

  1. 用 setMessage 方法灵活复用 UIAlertView

  2. C#编程(四十八)----------列表

    C#中的List C#中deList怎么样?List<T>类是ArrayList类的泛型等效类,该类使用大小可按需动态增长的数组实现List<T>泛型接口. 泛型的好处:它为使 ...

  3. 转: centos7.5 下 coredns+etcd搭建DNS服务器

    coredns简介 CoreDNS是一个DNS服务器,和Caddy Server具有相同的模型:它链接插件.CoreDNS是云本土计算基金会启动阶段项目. CoreDNS是SkyDNS的继任者. Sk ...

  4. 迷宫问题的C语言求解

    1 .Preface /** * There have been many data to introduce the algorithm. So I will try to simply expla ...

  5. 《Excel效率手册:早做完,不加班》

    <Excel效率手册:早做完,不加班> 基本信息 作者: 陈锡卢    杨明辉 出版社:清华大学出版社 ISBN:9787302350743 上架时间:2014-5-8 出版日期:2014 ...

  6. 帕累托分布(Pareto distributions)、马太效应

    什么是帕累托分布 帕累托分布是以意大利经济学家维弗雷多·帕雷托命名的. 是从大量真实世界的现象中发现的幂次定律分布.这个分布在经济学以外,也被称为布拉德福分布. 帕累托因对意大利20%的人口拥有80% ...

  7. 层次聚类 Hierarchical Clustering

    -------------------------------- 不管是GMM,还是k-means,都面临一个问题,就是k的个数如何选取?比如在bag-of-words模型中,用k-means训练码书 ...

  8. 奇怪吸引子---DequanLi

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  9. Combination Sum leetcode java

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...

  10. js中各种跨域问题实战小结

    什么是跨域?为什么要实现跨域呢?   这是因为JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.也就是说只能访问同一个域中的资源.我觉得这就有必要了解下javascript中的同源 ...