gpt4 book ai didi

linux - 为什么 linux shell diff 命令有时会显示与空行相关的输出,即使存在 -B (--ignore-blank-lines) 选项?

转载 作者:行者123 更新时间:2023-12-03 07:54:56 24 4
gpt4 key购买 nike

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.

最佳答案

-B 背后的全部原因是为了避免“仅空白”更改带来的噪音。 FreeBSD中的描述恕我直言,/macOS 手册页更清晰,因为它描述了“ block ”,因此很明显,它不适用于单行,特别是如果它们仅存在于比较的一侧。

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

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

关于linux - 为什么 linux shell diff 命令有时会显示与空行相关的输出,即使存在 -B (--ignore-blank-lines) 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76269590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com