如何防止GNU diff对补丁的行进行分组?

问题描述 投票:1回答:1

我从其他人那里继承了一组html文件的两个版本的维护,一个版本具有翻译功能,而另一个版本则具有较短的翻译功能。短文件和长文件的正文仅在包含翻译的行上有所不同。但是,在这些文件中还有一些不常见的特定内容,必须保持不变。

short.html

Salve<br/>
Quomodo te habes?<br/>
Some lines specific to short.html

long.html

Salve<br/>
Hello!<br/>
Quomodo te habes?<br/>
How do you do?<br/>
Some other lines specific to long.html

我用来编辑较短的文件,并且为了不重复两次,我查看与GNU diff的差异来创建补丁文件,以便对较大的文件应用相同的更正。

diff short.html.orig short.html > short.diff
patch -z .orig long.html short.diff

这很吸引人,因为patch足够聪明,可以跳过垃圾来寻找修补程序,直到我不得不纠正短文件中的[[相邻行。

在那种情况下,diff命令的输出如下:

1,2c1,2 < Salve<br/> < Quomodo te habes?<br/> --- > Salvete<br/> > Quomodo vos habetis?<br/>

并且该补丁被拒绝,因为我认为patch在long.html中找不到包含Salve...Quomodo...的两行的任何块以替换在long.html中,因为这两行在较长的时间内不再相邻每行之后插入翻译的版本。只要逐行应用补丁,它就可以很好地工作,但是当补丁被分组为一个块(它们称为块)时,它将不再起作用。

我的问题是:如何防止diff将要打补丁的行分组为块,以便逐行应用补丁?

不用多说,我希望将上面的diff命令的输出更改为:

1c1 < Salve<br/> --- > Salvete<br/> 2c2 < Quomodo te habes?<br/> --- > Quomodo vos habetis?<br/>

bash diff patch
1个回答
2
投票
我找到了解决问题的方法,而不是解决方法,在这种情况下,我不更改diff的行为。使用AWK,可以处理其输出以将具有多行的粗线切成一行的切片:

diffungroup

awk -F ',|c' ' # process lines as "3,4c3,4" and following lines /[0-9]+,[0-9]+c[0-9]+,[0-9]+/ { ss = $1; se = $2; ds = $3; de = $4; for (i = ss; i <= se; i++) { getline a[i] = $0 } getline # skip "---" i = ss for (j = ds; j <= de; j++) { print i "c" j print a[i++] print "---" getline print $0 } next } { print } ' "$@"
它像这样转换diff的输出:

1,2c1,2 < Salve<br/> < Quomodo te habes?<br/> --- > Salvete<br/> > Quomodo vos habetis?<br/>

进入:

1c1 < Salve<br/> --- > Salvete<br/> 2c2 < Quomodo te habes?<br/> --- > Quomodo vos habetis?<br/>

在以上问题中解释的上下文中,我以另一种方式调用它:

diff short.html.orig short.html > short.diff ./diffungroup short.diff > long.diff patch -z .orig long.html long.diff

而且,正如我在上面说的,它就像一种魅力。
© www.soinside.com 2019 - 2024. All rights reserved.