Bash:管道查找到 Grep

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36899854/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:34:10  来源:igfitidea点击:

Bash : Piping Find into Grep

bashgrepfind

提问by user2135970

The following command finds all occurrences of 'some string' by recursively searching through the current directory and all sub-directories

以下命令通过递归搜索当前目录和所有子目录来查找所有出现的“某个字符串”

grep -r -n  'some string' .

This command recursively searches through current directory and all sub-directories and returns all files of the form *.axvw

此命令递归搜索当前目录和所有子目录并返回 *.axvw 形式的所有文件

find . -name '*.axvw' 

I want to put these two commands together so I get all occurances of 'some string' by recursively searching through the current directory but only looking at files that end in 'axvw'.

我想将这两个命令放在一起,以便通过递归搜索当前目录但只查看以“axvw”结尾的文件来获取所有出现的“某个字符串”。

When I tried running the following command nothing was returned:

当我尝试运行以下命令时,没有返回任何内容:

find . -name '*js' | grep -n  'some string'

What am I doing wrong?

我究竟做错了什么?

回答by anubhava

You can use -execoption in find:

您可以-execfind以下选项中使用选项:

find . -name '*.axvw' -exec grep -n 'some string' {} +

Or else use xargs:

否则使用xargs

find . -name '*.axvw' -print0 | xargs -0 grep -n 'some string'

回答by sjwarner

find . -name '*js' -exec grep -n 'some string' {} \;

find . -name '*js' -exec grep -n 'some string' {} \;

Should work I think.

我认为应该工作。

Edit: just for fun, you could also use a double grep I believe.

编辑:只是为了好玩,我相信你也可以使用双 grep。

find . | grep 'some string' | grep js

find . | grep 'some string' | grep js