linux - grep 不显示路径/文件 :line

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

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

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

最佳答案

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

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

其中-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, --with-filename

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

https://stackoverflow.com/questions/19406761/

相关文章:

linux - 在 Unix 中,我可以在一个目录中运行 'make' 而不先 cd'ing 到那个

python - 字典和默认值

linux - 如何在 Linux 中为每个输出行列出一个文件名?

python - 如何从 Python 中的函数返回两个值?

c - socket编程中AF_INET和PF_INET有什么区别?

python - 省略号 [...] 在列表中是什么意思?

linux - 在 Unix/Linux 中判断两个文件是否具有相同内容的最快方法?

python - 转置列表列表

linux - 如何为 wget 设置代理?

python - 仅列出当前目录中的文件