简体   繁体   English

使用 find + cat 获取我“grep”的文件

[英]Obtain the file I "grep" with find + cat

I am trying to get the file name where the grepped text belongs.我正在尝试获取 grepped 文本所属的文件名。

I use this:我用这个:

find . -type f -name "*py" -exec cat -n {} \; | grep -H login.html

and I obtain this:我得到了这个:

(standard input):    11     url(r'^login$', LoginView.as_view(template_name='dojo/login.html', authentication_form=AuthenticationForm), name='login'),

Usually I use the option -a with cat also, when I get "standard input" log, but here, since I "cat" every file, I am not sure how to proceed.通常,当我获得“标准输入”日志时,我也会将选项-a与 cat 一起使用,但在这里,由于我“cat”每个文件,我不知道如何继续。

Regards.问候。

You can do this entirely with Grep您可以使用 Grep 完全做到这一点

grep -R -H --include="*.py" login.html

-R = Rescursion -R = 递归

-H = Add filenames -H = 添加文件名

--include= Glob for file (similar to your -name of find) --include= 全局文件(类似于您的查找名称)

I suggest using xargs: find. -type f -name "*py" | xargs grep -H login.html我建议使用 xargs: find. -type f -name "*py" | xargs grep -H login.html find. -type f -name "*py" | xargs grep -H login.html find. -type f -name "*py" | xargs grep -H login.html . find. -type f -name "*py" | xargs grep -H login.html You might need to use -print0 option in find and -0 in xargs if your file names contain spaces.如果您的文件名包含空格,您可能需要在find中使用-print0选项,在xargs中使用-0选项。 What xargs does is to pass output of the previous command (basically its own stdin) as command arguments to its argument xargs 所做的是将前一个命令(基本上是它自己的标准输入)的 output 作为命令 arguments 传递给它的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM