Why does linux shell diff command sometimes show output related to empty lines even though -B (–ignore-blank-lines) option is present?

huangapple go评论75阅读模式
英文:

Why does linux shell diff command sometimes show output related to empty lines even though -B (--ignore-blank-lines) option is present?

问题

Linux diff命令接受一个选项,该选项指示它“忽略所有空白行的更改”,如手册页所述。

尝试此选项时,我遇到了一个情况,即即使使用了-B(或--ignore-blank-lines)选项,空白行似乎仍然未被忽略。

以下是演示此问题的工作脚本:

#!/bin/bash

echo -e 'a\nb\nc' > file_a1
echo -e 'a\n\nb'  > file_a2

echo
echo \ file_a1
cat    file_a1
# a
# b
# c

echo
echo \ file_a2
cat    file_a2
# a
#
# b

echo
echo \ regular diff
diff    file_a1 file_a2
# 1a2
# >        <- output related to empty line
# 3d3
# < c

echo
echo \ diff -B : empty lines ignored, as expected
diff -B file_a1 file_a2
# 3d3
# < c

echo -e 'a\nb' > file_b1
echo -e '\nb'  > file_b2

echo
echo \ file_b1
cat    file_b1
# a
# b

echo
echo \ file_b2
cat    file_b2
#
# b

echo
echo \ regular diff
diff    file_b1 file_b2
# 1c1
# < a
# ---
# >        <- output related to empty line

echo
echo \ diff -B : same output, as if -B option was not present
diff -B file_b1 file_b2
# 1c1
# < a
# ---
# >        <- output related to empty line

echo
echo \ diff version
diff --version
# diff (GNU diffutils) 3.9
# Copyright (C) 2023 Free Software Foundation, Inc.
# License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
# 
# Written by Paul Eggert, Mike Haertel, David Hayes,
# Richard Stallman, and Len Tower.
英文:

Linux diff command accepts an option that instructs it to "ignore changes where lines are all blank", as per the manual page.

And while trying this option, I came across a situation where blank lines seem to not be ignored, even though -B (or --ignore-blank-lines) option is used.

Here follows a working script that demonstrates the issue:

#!/bin/bash

echo -e &#39;a\nb\nc&#39; &gt; file_a1
echo -e &#39;a\n\nb&#39;  &gt; file_a2

echo
echo \ file_a1
cat    file_a1
# a
# b
# c

echo
echo \ file_a2
cat    file_a2
# a
#
# b

echo
echo \ regular diff
diff    file_a1 file_a2
# 1a2
# &gt;        &lt;- output related to empty line
# 3d3
# &lt; c

echo
echo \ diff -B : empty lines ignored, as expected
diff -B file_a1 file_a2
# 3d3
# &lt; c

echo -e &#39;a\nb&#39; &gt; file_b1
echo -e &#39;\nb&#39;  &gt; file_b2

echo
echo \ file_b1
cat    file_b1
# a
# b

echo
echo \ file_b2
cat    file_b2
#
# b

echo
echo \ regular diff
diff    file_b1 file_b2
# 1c1
# &lt; a
# ---
# &gt;        &lt;- output related to empty line

echo
echo \ diff -B : same output, as if -B option was not present
diff -B file_b1 file_b2
# 1c1
# &lt; a
# ---
# &gt;        &lt;- output related to empty line

echo
echo \ diff version
diff --version
# diff (GNU diffutils) 3.9
# Copyright (C) 2023 Free Software Foundation, Inc.
# License GPLv3+: GNU GPL version 3 or later &lt;https://gnu.org/licenses/gpl.html&gt;.
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
# 
# Written by Paul Eggert, Mike Haertel, David Hayes,
# Richard Stallman, and Len Tower.

答案1

得分: 2

"-B 的整个原因是为了避免由于“仅空白字符”更改而产生的噪音。FreeBSD/macOS 手册中的描述在我看来更清晰,因为它描述了“块”,因此可以清楚地知道它不是用于单行,特别是如果它们只存在于比较的一侧。

关于什么构成更改的最初假设是不正确的,因为它还应该包括更改的类型(在块的顶部的那些神秘字符),以及指示如何应用该更改的信息。

您会注意到,在第一个测试中,操作“添加”或“删除”了一行空行,但在第二对中,它“更改”了一行,因此正如tripleee在他们的回答中解释的那样,这不是一个“仅空行”的操作。

英文:

The whole reason behind -B is to avoid noise from "white space only" changes. The description in the FreeBSD/macOS man page is IMHO clearer as it describes "chunks" so it is clear that it is not meant to apply to single lines, specially if they only exist in one side of the comparison.

Your original assumption about what constitutes a change is incorrect, as it should ALSO include the type of change (those cryptic characters on top of the chunk), and which indicate how that change would apply.

You would notice that for your first test, the operation "adds" or "deletes" an empty line, but in the second pair, it "changes" a line and therefore as tripleee explains in their answer it is not a "blank line only" operation.

答案2

得分: 1

根据man页面的说明,-B 仅适用于空白字符发生变化的情况。在您的 file_b* 示例中,某处变为空行,因此“所有行都是空白的”是不准确的。

您可以提出描述模糊的观点,但我认为它的意思是这样的。

英文:

Like the man page tells you, -B applies where the only changes are in whitespace. In your file_b* example, something changed into an empty line, so "lines are all blank" is untrue.

You could argue that the description is ambiguous, but this is what I believe it means.

huangapple
  • 本文由 发表于 2023年5月17日 15:36:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定