在不同情况下理解Grep的选项-H的实际用法 [英] To understand the practical use of Grep's option -H in different situations
问题描述
这个问题基于这个答案。
为什么您会从两个命令中获得相同的输出?
$ b 命令A
$ sudo grep muel * / tmp
masi:muel
命令B
$ c $ $ sudo grep -H muel * / tmp
masi:muel
Rob的评论建议我命令A 不应该给我 masi:
,但只有 muel
。
总之, -H
?
如果给出多个文件名,Grep会默认列出文件名。即使只给出一个文件名, -H
选项也会这样做。在这两个例子中,给出了多个文件名。
这里有一个更好的例子:
$ grep Richie notes.txt
Richie戴眼镜。
$ grep -H Richie notes.txt
notes.txt:Richie戴眼镜。
当您为未知数量的文件提供通配符时,它会更加有用,并且您始终即使通配符只匹配一个文件,也希望打印文件名。
This question is based on this answer.
Why do you get the same output from the both commands?
Command A
$sudo grep muel * /tmp
masi:muel
Command B
$sudo grep -H muel * /tmp
masi:muel
Rob's comment suggests me that Command A should not give me masi:
, but only muel
.
In short, what is the practical purpose of -H
?
Grep will list the filenames by default if more than one filename is given. The -H
option makes it do that even if only one filename is given. In both your examples, more than one filename is given.
Here's a better example:
$ grep Richie notes.txt
Richie wears glasses.
$ grep -H Richie notes.txt
notes.txt:Richie wears glasses.
It's more useful when you're giving it a wildcard for an unknown number of files, and you always want the filenames printed even if the wildcard only matches one file.
这篇关于在不同情况下理解Grep的选项-H的实际用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!