在常见的东西上修补略有不同的文件


1

我已经尝试过使用常规差异和补丁来执行以下操作,但它 似乎不起作用或我没有正确使用它。我想知道是否有 是一种根据实际行内容修补文件更改的工具。
例如,我有以下类似的文件与公共行,但 不同的行顺序:

  File 1.txt
  Hi, my name is Ted.
  1
  2
  3
  4

  File 2.txt
  1
  2
  3
  4
  Hi, my name is Ted


  File 3.txt
  1
  2
  Hi, my name is Ted
  3
  4


  I change File 1.txt to:
  Hi, my name is Joe.
  1
  2
  3
  40

现在我想创建某种补丁或将此更改分发给其他文件,但不是通过将File 1.txt的全部内容复制到其他文件,而是仅通过查找常用行:

  4 

  40

  Hi, my name is Ted. 

  Hi, my name is Joe.

任何帮助,赞赏,

摊晒

Answers:


1

不同的操作系统可能有完全不同的工具

Sed(存在于* nix上,移植到Win,对OSX一无所知)

从例子

 # substitute (find and replace) "foo" with "bar" on each line

 sed 's/foo/bar/'             # replaces only 1st instance in a line

即smth。喜欢

sed 's/4/40/' 2.txt > 2e.txt
...
sed 's/4/40/' 4.txt > 4e.txt

要么

sed 's/4/40/' 2.txt 3.txt 4.txt> common.txt

并拆分合并后的common.txt

差异(见上文)

准备两个中间编辑版本的1.txt

1a将是

  Hi, my name is Joe.
  1
  2
  3
  4

1b将是

  Hi, my name is Ted.
  1
  2
  3
  40

准备补丁

diff -U 0 1.txt 1a.txt > fix1.patch

diff -U 0 1.txt 1b.txt > fix2.patch

应用补丁

patch -u -b 2.txt fix1.patch
...
patch -u -b 3.txt fix2.patch

未测试头部命令的结果

awk脚本(见上文)

GrepWin (仅限Windows)

编写搜索替换模式(支持regexps),测试,存储在预设中

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.