我有两个文件想要比较。这些行有时间戳,可能还有一些我想在匹配算法中忽略的其他内容,但如果匹配算法发现文本的其余部分存在差异,我仍然希望输出这些项目。例如:
1c1
< [junit4] 2013-01-11 04:43:57,392 INFO com.example.MyClass:123 [main] [loadOverridePropFile] Config file application.properties not found: java.io.FileNotFoundException: /path/to/application.properties (No such file or directory)
---
> [junit4] 2013-01-11 22:16:07,398 INFO com.example.MyClass:123 [main] [loadOverridePropFile] Config file application.properties not found: java.io.FileNotFoundException: /path/to/application.properties (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
不应该被发射,但是:
1c1
< [junit4] 2013-01-11 04:43:57,392 INFO com.example.MyClass:123 [main] [loadOverridePropFile] Config file application.properties not found: java.io.FileNotFoundException: /path/to/application.properties (No such file or directory)
---
> [junit4] 2013-01-11 22:16:07,398 INFO com.example.MyClass:456 [main] [loadOverridePropFile] Config file application.properties not found: java.io.FileNotFoundException: /path/to/application.properties (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
应该发出(因为行号不同)。请注意,时间戳仍然会发出。
如何才能做到这一点?
假设您的文件是“a.txt”和“b.txt”。您可以这样使用 diff + cut 来获取它:
diff <(cut -d" " -f4-99 a.txt) <(cut -d" " -f4-99 b.txt)
Run Code Online (Sandbox Code Playgroud)
每次剪切都会忽略前 3 个字段(与日期和此类内容相关),只考虑该行的其余部分(从第 4 列到第 99 列)。剪切应该使用以下方式工作:
cut -d" " -f4- a.txt
Run Code Online (Sandbox Code Playgroud)
但它对我不起作用,所以我添加了-f4-99。因此,我们对两个输入应用 cut 以忽略日期字段,然后运行 diff 来根据需要对它们进行比较。