grep没有显示路径/文件:行 [英] grep without showing path/file:line

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

问题描述

你怎么grep,只返回匹配的行?即在结果中省略了路径/文件名。



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



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


解决方案

不需要 find 。如果你只是在一个特定的目录下寻找一个模式,这应该就足够了:

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

其中 -h 是参数隐藏文件名,从 man grep


h,--no-filename



禁止输出文件名的前缀。这是默认的
,当只有一个文件(或只有标准输入)要搜索。

请注意,我们使用


-H,--with-filename

打印每个比赛的文件名称。这是
多于一个文件进行搜索时的默认值。



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

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

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.

Note that you we using

-H, --with-filename

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆