微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

shell – 为什么使用忽略匹配行的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}}

我正在运行以下命令:

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.

这种行为确实看起来有点奇怪.我通过调整输入文件注意到了一些事情(我只是将“Memory”行移到了两个文件的顶部):

FILE1.TXT

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

FILE2.TXT

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

普通的差异会给你:

diff file1.txt file2.txt

4c4
< Tab=> 'Memory' Title=> {text {Total memory allocated: 962192 kB}}
---
> Tab=> 'Memory' Title=> {text {Total memory allocated: 9621932 kB}}
7c7
< Tab=> 'cpu' Title=> {text {Total cumulative cpu time: 9030 ms}}
---
> Tab=> 'cpu' Title=> {text {Total cumulative cpu time: 90303 ms}}

请注意,现在有两组差异……使用这种安排,diff -I’Memory’file1.txt file2.txt命令将起作用并输出

7c7
< Tab=> 'cpu' Title=> {text {Total cumulative cpu time: 9030 ms}}
---
> Tab=> 'cpu' Title=> {text {Total cumulative cpu time: 90303 ms}}

意思是,-I标志似乎仅在一组差异中的每一行与表达式匹配时才起作用.我不知道这是一个错误或预期的行为……但它肯定是不一致的.

编辑:实际上,根据GNU diff documentation,它是预期的行为.手册页不太清楚. OpenBSD diff也有-I标志,但their man page更好地解释了它.

原文地址:https://www.jb51.cc/bash/386971.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐