为什么使用忽略匹配行的diff无法按预期工作? [英] Why diff with ignore matching lines doesn't work as expected?

查看:100
本文介绍了为什么使用忽略匹配行的diff无法按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件:

file1.txt

###################################################
Dump stat Title information for 'ssummary' view
###################################################
Tab=> 'Instance' Title=> {text {Total instances: 7831}}
Tab=> 'Device' Title=> {text {Total spice devices: 256}}
Tab=> 'Memory' Title=> {text {Total memory allocated: 962192 kB}}
Tab=> 'Cpu' Title=> {text {Total cumulative CPU time: 9030 ms}}

file2.txt

###################################################
Dump stat Title information for 'ssummary' view
###################################################
Tab=> 'Instance' Title=> {text {Total instances: 7831}}
Tab=> 'Device' Title=> {text {Total spice devices: 256}}
Tab=> 'Memory' Title=> {text {Total memory allocated: 9621932 kB}}
Tab=> 'Cpu' Title=> {text {Total cumulative CPU time: 90303 ms}}

我正在运行以下命令:

And I'm running the following command:

diff -I 'Memory' file1.txt file2.txt

输出:

6,7c6,7
< Tab=> 'Memory' Title=> {text {Total memory allocated: 962192 kB}}
< Tab=> 'Cpu' Title=> {text {Total cumulative CPU time: 9030 ms}}
---
> Tab=> 'Memory' Title=> {text {Total memory allocated: 9621932 kB}}
> Tab=> 'Cpu' Title=> {text {Total cumulative CPU time: 90303 ms}}

但是我的预期输出是:

< Tab=> 'Cpu' Title=> {text {Total cumulative CPU time: 9030 ms}}
---
> Tab=> 'Cpu' Title=> {text {Total cumulative CPU time: 90303 ms}}

请注意,如果我更改了'Memory''Tab''Title'问题已解决,但可能所有行均被忽略,因为它们都具有Tab和Title。

Note that in the command if I change 'Memory' to 'Tab' or 'Title' problem's solved, but probably all lines are ignored cause they all have Tab and Title.

推荐答案

鉴于 diff 的工作方式,这种行为是正常的(截至2013年4月)。

This behaviour is normal given the way diff works (as of April 2013).

diff 是面向行的,这意味着一行被视为完全不同或完全等效。如果忽略某行,则在比较之前将其输入到不同行的列表中,并且当计算更改脚本时,仅由忽略的行进行的更改本身被视为已忽略。当忽略的行与更改的行相邻时,它构成一个不可忽略的更改。

diff is line oriented, it means that a line is either considered totally different or totally equivalent. When a line is ignored, it is entered into the list of different lines before comparison, and when the change script is computed, changes made only of ignored lines are considered themselves as ignored. When ignored lines are adjacent to changed lines, it makes up a single non-ignored change.

问题在于无法使用 diff 以了解连续的行不相关:您不是在区分文本序列( diff 的目的是什么),而是一列独立的行键( Tab> =< key> )。当两个文件以相同顺序生成,但仍然不相同时,这些问题看起来非常相似。

The problem lies in the inability of diff to understand that consecutive lines are not related: you are not diffing a sequence of text (what diff is aimed at), but rather a list of independent lines which are keyed (Tab >= <key>). These problems seem pretty similar when both files are generated in the same order, but still not the same.

这篇关于为什么使用忽略匹配行的diff无法按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆