自动化目录差异,同时忽略文件中的某些特定行

时间:2011-09-22 16:00:15

标签: python regex diff araxis

我需要比较两个目录,并产生某种结构化输出(文本文件很好)的差异。也就是说,输出可能如下所示:

file1 exists only in directory2
file2 exists only in directory1
file3 is different between directory1 and directory2

我不关心格式,只要信息在那里。第二个要求是我需要能够在区分两个文件时忽略某些字符序列。 Araxis Merge具有以下功能:您可以输入正则表达式,并且任何文件的唯一区别在于与正则表达式匹配的字符序列将被报告为相同。

这会让Araxis Merge成为一个很好的候选者,但是,到目前为止,我还没有办法生成差异的结构化输出。即使使用命令行参与者启动consolecompare.exe,它也会打开一个显示差异的Araxis GUI窗口。

那么,是否存在以下任何一种情况?

  • 让Araxis Merge将差异结果打印到文本文件的方法吗?
  • 另一个在忽略某个字符时执行diff的实用程序 序列,并产生结构化输出?

如果这样的实用程序作为Python的模块或插件存在,则会获得额外的功劳。请记住,这必须完全从命令行/ python脚本完成 - 没有GUI。

1 个答案:

答案 0 :(得分:1)

在某种程度上,普通的旧diff命令可以做到这一点,即比较目录内容并忽略与某个正则表达式模式匹配的更改(使用-I选项)。

来自man bash

-I regexp
      Ignore changes that just insert or delete lines that match  regexp.

快速演示:

[me@home]$ diff images/ images2
Only in images2: x
Only in images/: y
diff images/z images2/z
1c1
< zzz
---
> zzzyy2

[me@home]$ # a less verbose version
[me@home]$ diff -q images/ images2
Only in images2: x
Only in images/: y
Files images/z and images2/z differ

[me@home]$ # ignore diffs on lines that contain "zzz"
[me@home]$ diff -q -I ".*zzz.*" images/ images2/
Only in images2/: x
Only in images/: y
相关问题