linux -- Linux diff与patch的深入分析

diff的输出格式分为传统格式和统一格式

 

1)diff的传统格式输出.

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

  cat before.txt

  输出:

  This is a line to be deleted

  This is a line that will be changed

  This is a line that will be unchanged

  cat after.txt

  输出:

  This is a line that has been changed

  This is a line that will be unchanged

  This is a line that has been added

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

  diff before.txt after.txt

  输 出:

  1,2c1

  < This is a line to be deleted

  < This is a line that will be changed

  ---

  > This is a line that has been changed

  3a3

  > This is a line that has been added

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

  注释:

  传统格式的输出

  1,2c1是指替换第1个文件的第1,2行到第2个文件的第2行,这里的1,2是指第1个文件的第1,2行,c是替换的意思,最后的1是第2个文件的第1行

  <号是指第1个文件更改或删除的行

  ---号是分割两个文件

  >号是第2个文件中增加或删除的行

  3a3是指将第2个文件的第3行插入到第一个文件的第3行

  也就是说第1个文件的:

  < This is a line to be deleted

  < This is a line that will be changed

  被替换成第2个文件的:

  > This is a line that has been changed

  由于第1个文件的第3行和第2个文件的第2行一致,所以不做修改.

  由于第2个文件的第3行是第1个文件所不具有的,所以在第1个文件的最后一行增加:

  > This is a line that has been added

2)patch命令的应用

  用diff的传统格式输出:

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

  diff before.txt after.txt >mypatch.txt

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

  用patch修补before.txt文件,使before.txt和after.txt一致.

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

  cat mypatch.txt |patch before.txt

  输出:

  patching file before.txt

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

  比较两个文件,现在是一致的了.

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

  cmp before.txt after.txt

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

  用patch命令恢复before.txt.

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

  patch -R before.txt <mypatch.txt

  输出:

  patching file before.txt

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

  注:-R标记告诉patch在反向上应用区别或者撤销patch.

  再比较两个文件,现在不一致了.

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

  cmp before.txt after.txt

  输出:

  before.txt after.txt differ: byte 17, line 1

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

3)diff的统一格式输出.

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

  diff -u before.txt after.txt |tee mypatch.diff

  输出:

  --- before.txt  2009-06-20 05:21:49.000000000 +0800

  +++ after.txt   2009-06-20 04:03:16.000000000 +0800

  @@ -1,3 +1,3 @@

  -This is a line to be deleted

  -This is a line that will be changed

  +This is a line that has been changed

  This is a line that will be unchanged

  +This is a line that has been added

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

  注释:

  diff -u选项是统一格式输出.

  --- before.txt  2009-06-20 05:21:49.000000000 +0800

  --- before.txt是指旧文件

  +++ after.txt   2009-06-20 04:03:16.000000000 +0800

  +++ after.txt是指新文件.

  @@ -1,3 +1,3 @@

  @@ -1,3是指第1个文件一共有3行,+1,3 @@是指第2个文件一共有3行.

  -This is a line to be deleted

  -This is a line that will be changed

  是被删除的行

  +This is a line that has been changed

  是增加的行

  This is a line that will be unchanged

  没有-号和+号是指该行不变,因为after.txt和before.txt都有这行.

  +This is a line that has been added

  是增加的行

  diff的统一格式比较与输出是按顺序进行的.

  4)diff命令在目录中的应用.

  新建old和new目录,old目录包含了初始内容,new目录包含文件的最新版本.

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

  mkdir old new

  echo "This is one. It's unchanged." | tee old/one new/one

  echo "This is two. It will change." > old/two

  echo "This is two. It changed.">new/two

  echo "This is three. It's new" > new/three

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

  创建修补文件

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

  diff -Nur old/ new/ >mypatch.diff

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

  注:-r选项按照文件目录递归创建修补文件.

  -u还是统一模式

  -N是指当diff遇到一个只存在于两个树中的一个树中的文件时,默认情况下跳过文件并且打印一个警告到stderr.

  这个行为可以通过-N选项来更改,这也导致了diff认为丢失的文件实际上是存在的,但它是空的.采用这种方式,

  一个修补文件可以包括已经创建的文件.然后应用修补程序创建新的文件.

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

  more mypatch.diff

  输出:

  diff -Nur old/three new/three

  --- old/three   1970-01-01 08:00:00.000000000 +0800

  +++ new/three   2009-06-20 06:55:34.000000000 +0800

  @@ -0,0 +1 @@

  +This is three. It's new

  diff -Nur old/two new/two

  --- old/two     2009-06-20 06:55:08.000000000 +0800

  +++ new/two     2009-06-20 06:55:21.000000000 +0800

  @@ -1 +1 @@

  -This is two. It will change.

  +This is two. It changed.

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

注释:

  diff -Nur old/three new/three是指下面比较old/three new/three两个文件.

  因为没有old/three文件,所以在old/three中增加+This is three. It's new

  diff -Nur old/two new/two是指下面比较old/two new/two两个文件

  因为old/two与new/two的第3行不一致,所以删除This is two. It will change.增加This is two. It changed.

  打补丁到old目录,新建old/three以及更改old/two

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

  patch --dir old< mypatch.diff

  ls -l     old/

  输出:

  one    three  two

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

  恢复old目录的内容,包括删除old/three,以及恢复old/two文件

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

  patch --dir old -R <mypatch.diff

  输出:

  ls -l old/

  one  two

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

  5)检查和合并更改

  用vim突出显示单个字符的更改来表示区别.

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

  vim -d after.txt before.txt

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

  用gui工具gvimdiff来显示两个文件.

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

  gvimdiff after.txt before.txt

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

  新建文件orig.c

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

  vi orig.c

  void foo(void)

  {

  printf("This will be changed by me. \n");

  printf("This will be unchanged,\n");

  printf("This will be changed by you.\n");

  }

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

  复制文件orig.c到me.c,更改第4行为printf("This was changed by me. \n");

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

  vi me.c

  void foo(void)

  {

  printf("This was changed by me. \n");

  printf("This will be unchanged,\n");

  printf("This will be changed by you.\n");

  }

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

  复制文件orig.c到you.c,更改第7行为printf("This was changed by you.\n");

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

  vi you.c

  void foo(void)

  {

  printf("This will be changed by me. \n");

  printf("This will be unchanged,\n");

  printf("This was changed by you.\n");

  }

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

  版本工具如cvs,subversion使用GNU合并工具称为diff3.

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

  diff3 me.c orig.c you.c

  输出:

  ====1

  1:3c

  printf("This was changed by me. \n");

  2:3c

  3:3c

  printf("This will be changed by me. \n");

  ====3

  1:7c

  2:7c

  printf("This will be changed by you.\n");

  3:7c

  printf("This was changed by you.\n");

  注:

  在没有参数的情况下,diff3产生的输出说明了那行更改.

  ====1和====3指明造成同原始文件不同的是哪一个修改文件.

  编号方式基于参数序列.

  也就是第1个文件和第3个文件与原文件不同.

  1:3c

  printf("This was changed by me. \n");

  3:3c

  printf("This will be changed by me. \n");

  1:3c表示第1个文件的第3行与3:3c表示的第3个文件的第3行不同.

  为什么不显示与原文件的比较呢。因为第3个文件的第3行与源文件(第2个文件)相同.所以与哪个文件比较无所谓了.

  2:7c

  printf("This will be changed by you.\n");

  3:7c

  printf("This was changed by you.\n");

  2:7c表示第2个文件的第7行与3:7c表示的第3个文件的第7行不同.

  diff3会试图为我们进行合并.合并是在源文件的基础上,依据两个新文件进行修改

  源文件是第二个文件,第一个文件和第三个文件可以互换,但他们必须有共同的祖先,就是第二个文件.

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

  diff3 -m me.c orig.c you.c |cat -n

  输出:

  1    void foo(void)

  2    {

  3        printf("This was changed by me. \n");

  4

  5        printf("This will be unchanged,\n");

  6

  7        printf("This was changed by you.\n");

  8    }

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

为了测试更复杂的环境,新建一个文件orig.c.1

  内容如下:

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

  vi orig.c.1

  void foo(void)

  {

  printf("This will be changed by both of us.\n");

  }

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

  用diff3 -m再次比较输出,如下:

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

  diff3 -m me.c orig.c.1 you.c

  void foo(void)

  {

  <<<<<<< me.c

  printf("This was changed by me. \n");

  printf("This will be unchanged,\n");

  printf("This will be changed by you.\n");

  ||||||| orig.c.1

  printf("This will be changed by both of us.\n");

  =======

  printf("This will be changed by me. \n");

  printf("This will be unchanged,\n");

  printf("This was changed by you.\n");

  >>>>>>> you.c

  }

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

  注释:以上的格式,同cvs update,需要人工合并文件的格式是一致的.

转载于:https://www.cnblogs.com/zym0805/archive/2012/02/15/2352830.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/275007.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

shell命令之---sed

1. sed编辑器基础 1.1 替换标记 命令格式&#xff1a;s/pattern/replacement/flags $ cat data4.txt    This is a test of the test script.    This is the second test of the test script.    有4种可用的替换标记&#xff1a; 数字&#xff0c;表明新文本将替…

SEE Conf: Umi 4 设计思路文字稿

大家好&#xff0c;我是若川。持续组织了5个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。复制此链接 https:…

用户体验改善案例_改善用户体验研究的5种习惯

用户体验改善案例There’s plenty of misunderstanding around user research, whether it’s the concept of validation or one-off anecdotes being thrown around as concrete evidence for a product decision.用户研究存在很多误解&#xff0c;无论是验证的概念还是一次性…

一场赛跑引起的并发知识

享学特邀作者&#xff1a;老顾前言我们小伙伴们是不是经常需要测试代码的性能&#xff1f;小伙伴们是不是就会想到jmeter进行压力测试一下&#xff0c;模拟N个用户同时执行下&#xff0c;看看响应的时间多少。今天老顾就用一个经典的比赛案例&#xff0c;来尝试自行编写个比赛业…

oracle中使用子查询为何取到大于自然数1 rownum 浅度解析

Oracle 没有提供TOP N 语句&#xff0c;若希望按特定条件查询前N 条记录&#xff0c;可以使用伪列ROWNUM。 ROWNUM 是对结果集加的一个伪列&#xff0c;即先查到结果集之后再加上去的一个列(注意&#xff1a;先要 有结果集)。 rownum 的值是oracle 顺序分配的从查询返回的行的编…

巴克莱对冲_“巴克莱的财政预算案”:使金钱管理对心理健康有效—用户体验案例研究

巴克莱对冲Disclaimer: all official Barclays assets used for this project are purely for educational/project purposes only and do not reflect the intentions of Barclays or any of its affiliates.免责声明&#xff1a;用于此项目的所有官方巴克莱资产纯粹是出于教育…

6 个对所有 Web 开发者都有用的 GitHub 仓库

作者&#xff1a;Mehdi Aoussiad原文&#xff1a;https://javascript.plainenglish.io/6-useful-github-repositories-for-all-web-developers-44f26912fd66大家好&#xff0c;我是若川。持续组织了5个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&…

快速删除数据库中所有表中的数据

今天又学到一招&#xff0c;可以快速删除数据库中所有的用户表中的数据。我是个菜鸟&#xff0c;望各位大神多多指教 select truncate table Name ; from sysobjects where xtypeU order by name asc; 该条语句执行之后会将数据库中所有的表都查询出来&#xff0c;复制出来之…

openfiler的iSCSI配置(二)

为什么80%的码农都做不了架构师&#xff1f;>>> 一.openfiler iSCSI配置 1.启动iSCSI target server服务。在Services列表下。 2.设置访问列表。在System---Network Access Configuration下设置。 3.创建卷设备 二.ISCSI客户端配置 1.安装open-iscsi # apt-get ins…

送你一份用Electron开发桌面应用的避坑指南【送3本书,含犀牛书】

大家好&#xff0c;我是若川。持续组织了5个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;新年第一次送3本书。抽奖规则见文末。如今&#xff0c;Electron 领域发生了重大的变革&#xff0c;Electron 版本更新换代极快&#xff0c;难以计数…

时间续

mois : janvier fvrier mars avril mai juin juillet aot septembre octobre novembre dcembre semaine : lundi mardi mercredi jeudi vendredi samedi dimanche 转载于:https://www.cnblogs.com/lavieenrose/archive/2012/02/18/2357597.html

nginx修改upstream不重启的方法(ngx_http_dyups_module模块)

为什么80%的码农都做不了架构师&#xff1f;>>> nginx很强大&#xff0c;第三方模块也不少,淘宝在nginx上很活跃&#xff0c;特别是章亦春&#xff0c;他参与的模块至少10&#xff0c; 好了今天主角不是他&#xff0c;是一款动态配置upstream的模块&#xff0c;这个…

c# 设计原则需要学习吗_向最好的学习:产品设计原则

c# 设计原则需要学习吗重点 (Top highlight)In my job as Design Team Lead at SimpleSite, I’ve recently been part of creating a set of Product Design Principles. In this process, I spent a lot of time studying the theory, learning about best practices, and ge…

初学Java-接口

在Java语言中&#xff0c;接口有两种意思&#xff1a; 一是指概念性的接口&#xff0c;即指系统对外提供的所有服务。类的所有能被外部使用者访问的方法构成了类的接口 二是指用interface关键字定义的实实在在的接口&#xff0c;也称为接口类型。它用于明确的描述系统对外提供的…

Node.js 2021年开发者报告解读

大家好&#xff0c;我是若川。持续组织了5个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。很多人觉得Node.js…

搭建nginx反向代理用做内网域名转发

为什么80%的码农都做不了架构师&#xff1f;>>> 情景 由于公司内网有多台服务器的http服务要映射到公司外网静态IP&#xff0c;如果用路由的端口映射来做&#xff0c;就只能一台内网服务器的80端口映射到外网80端口&#xff0c;其他服务器的80端口只能映射到外网的…

外国经典儿童读物合集pdf_帮助父母在线购买儿童读物–用户体验案例研究

外国经典儿童读物合集pdfTŤ As our first group project at GA, we needed to quickly learn how to use several online tools that helped our team of 4 collaborate and communicate while socially distant. Despite the rather extreme circumstances our team was stil…

Windows Azure Marketplace增加对六种语言和HTML5应用程序的支持

对于那些不熟悉Windows Azure Marketplace的人来说&#xff0c;它是一个供数据供应商和开发人员购买和销售数据集和应用程序的在线市场。 可以在世界上的26个国家使用Windows Azure Marketplace&#xff0c;它现在支持6种语言&#xff0c;包括日语、汉语、法语、德语、西班牙语…

如何优雅处理 async await 错误——解读小而美的 await-to-js 库

大家好&#xff0c;我是若川。持续组织了5个月源码共读活动&#xff0c;感兴趣的可以点此加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。同时极力推荐订阅我写的《学习源码整体架构系列》 包含20余篇源码文章。这是源码共读活动第…

同态加法_同态的Spotify

同态加法重点 (Top highlight)When neumorphism was predicted to be one of the top 2020 UI design trends, I wanted to give it a shot. Having said that, I wanted to explore a type that had not gone overboard, neumorphism in Dark Mode.当neumorphism预计为顶部202…