当运行下面的grep命令时我得到了奇怪的结果,首先“1.txt”的内容是这样的:
401 f
-- -----
然后当使用cat 1.txt | grep -v -- --
,结果为401 f
,命令cat 1.txt | grep -v -- -
,结果也是401 f
。我知道第一个字段中的 --
是 grep 的过滤模式,所以我认为第二个字段中的 --
和 -
可能都意味着标准输出,对吗?然后我运行 cat 1.txt | grep -v --
,这里我省略了标准输出,但它是错误的,信息是 Try 'grep --help' for more information
。同时我还运行 cat 1.txt | grep -v -
,这有效,结果是 401 f
。
为了进一步了解 grep 中 --
和 -
之间的区别,我运行 cat 1.txt | grep --
,它显示 Try 'grep --help' for more information
。 cat 1.txt | grep -
获取结果 -- -----
.
顺便说一句,我试过了
cat 1.txt | grep -v '--' --
cat 1.txt | grep -v '--' -
cat 1.txt | grep -v '--'
cat 1.txt | grep -v '-'
cat 1.txt | grep '--'
cat 1.txt | grep '-'
最后,我得到了相同的对应结果。
我的问题可能是
第二个字段的
--
和-
有什么区别?为什么我不能使用
--
模式在行中得到-- -----
?
最佳答案
--
在使用 getopt 的实用程序中用于指示选项结束。
来自manual :
The special argument "--" forces an end of option-scanning regardless of the scanning mode.
因此在实践中,您的第一个 --
参数将被忽略。如果你想为 --
做 grep,你需要 grep .... -- --
关于linux - 如何理解带有 “--” 符号的这个奇怪的 grep 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36528008/