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

发布于 2024-12-05 17:47:17 字数 645 浏览 4 评论 0原文

我需要比较两个目录,并生成某种差异的结构化输出(文本文件很好)。也就是说,输出可能看起来像这样:

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 将差异结果打印到文本文件的方法?
  • 另一个在忽略某些字符的情况下进行比较的实用程序 序列,并产生结构化输出?

如果这样的实用程序作为 Python 的模块或插件存在,则额外加分。请记住,这必须完全通过命令行/Python 脚本完成 - 没有 GUI。

I need to compare two directories, and produce some sort of structured output (text file is fine) of the differences. That is, the output might looks something like this:

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

I don't care about the format, so long as the information is there. The second requirement is that I need to be able to ignore certain character sequences when diffing two files. Araxis Merge has this ability: you can type in a Regex and any files whose only difference is in character sequences matching that Regex will be reported as identical.

That would make Araxis Merge a good candidate, BUT, as of yet I have found no way to produce a structured output of the diff. Even when launching consolecompare.exe with command-line argumetns, it just opens an Araxis GUI window showing the differences.

So, does either of the following exist?

  • A way to get Araxis Merge to print a diff result to a text file?
  • Another utility that do a diff while ignoring certain character
    sequences, and produce structured output?

Extra credit if such a utility exists as a module or plugin for Python. Please keep in mind this must be done entirely from a command line / python script - no GUIs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2024-12-12 17:47:17

在某种程度上,普通的旧 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

To some extent, the plain old diff command can do just that, i.e. compare directory contents and ignoring changes that match a certain regex pattern (Using the -I option).

From man bash:

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

Quick demo:

[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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文