本文介绍了grep 不显示路径/文件:行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何 grep 并只返回匹配的行?即结果中省略了路径/文件名.

How do you grep and only return the matching line? i.e. The path/filename is omitted from the results.

在这种情况下,我想查看当前目录中的所有 .bar 文件,搜索词 FOO

In this case I want to look in all .bar files in the current directory, searching for the term FOO

find . -name '*.bar' -exec grep -Hn FOO {} ;

推荐答案

无需查找.如果您只是在特定目录中寻找模式,这应该足够了:

No need to find. If you are just looking for a pattern within a specific directory, this should suffice:

grep -hn FOO /your/path/*.bar

其中-h是隐藏文件名的参数,来自man grep:

Where -h is the parameter to hide the filename, as from 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, --with-filename

打印每个匹配项的文件名.这是默认的,当有要搜索的文件不止一个.

Print the file name for each match. This is the default when there is more than one file to search.

这篇关于grep 不显示路径/文件:行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:39