从 diff 重建文件

从 diff 重建文件

假设我有一个文件foo和一个文件bar。我现在调用以下命令:

$ diff foo bar > diff_foobar
$ rm bar

是否有一种(简单的)方法可以以编程方式重建和bar中缺失的内容?foodiff_foobar

答案1

从差异重建文件,也称为“应用差异”,是实用程序的作用patch

## Create two arbitrary files, `original' and `altered':
$ seq 0 3 >original
$ seq 7 11 >altered

## Compute a diff from `original' to `altered':
$ diff original altered >diff

## Copy `original' before patching it
$ cp -vi original original.0
`original' -> `original.0'
$ patch <diff original # This modifies `original'!
patching file original

## Confirm that we rebuilt the file from `original' and `diff`
$ diff --report-identical-files original altered
Files original and altered are identical

patch 命令还有很多选项,特别是反向应用补丁。

答案2

是的,您应用补丁:

你做了:

$ diff foo bar > diff_foobar
$ rm bar

现在做:

$ patch  foo -o bar < diff_foobar

相关内容