shell – 为什么使用忽略匹配行的diff不能按预期工作?

前端之家收集整理的这篇文章主要介绍了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.f2er.com/bash/386971.html

猜你在找的Bash相关文章