Linux grep 不显示路径/文件:行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19406761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grep without showing path/file:line
提问by Allan Thomas
How do you grep and only return the matching line? i.e. The path/filename is omitted from the results.
你如何 grep 并只返回匹配的行?即结果中省略了路径/文件名。
In this case I want to look in all .bar files in the current directory, searching for the term FOO
在这种情况下,我想查看当前目录中的所有 .bar 文件,搜索术语 FOO
find . -name '*.bar' -exec grep -Hn FOO {} \;
采纳答案by fedorqui 'SO stop harming'
No need to find
. If you are just looking for a pattern within a specific directory, this should suffice:
没有必要find
。如果您只是在特定目录中寻找模式,这应该就足够了:
grep -hn FOO /your/path/*.bar
Where -h
is the parameter to hide the filename, as from man grep
:
-h
隐藏文件名的参数在哪里,从man grep
:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.
-h, --no-文件名
禁止输出文件名的前缀。当只有一个文件(或只有标准输入)要搜索时,这是默认设置。
Note that you were using
请注意,您正在使用
-H, --with-filename
Print the file name for each match. This is the default when there is more than one file to search.
-H, --with-filename
打印每个匹配项的文件名。这是要搜索多个文件时的默认设置。
回答by TC1
From the man page:
从手册页:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there
is only one file (or only standard input) to search.
回答by jkshah
Just replace -H
with -h
. Check man grep
for more details on options
只需替换-H
为-h
. 检查man grep
有关选项的更多详细信息
find . -name '*.bar' -exec grep -hn FOO {} \;