协助“查找”和“grep”命令

发布于 2025-01-05 01:30:32 字数 2036 浏览 4 评论 0原文

我正在寻求有关可以从 Mac OS X 终端运行的单行代码的帮助。我在 Mac 上使用 MAMP 进行 Web 开发。我的“/Applications/MAMP/htdocs”目录中有很多 CakePHP 项目。为了简单起见,我们假设我有两个 CakePHP 项目,这是 find /Applications/MAMP/htdocs -type d -iname Controller* 命令的输出:

/Applications/MAMP/htdocs/my_cake1.3_project/app/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/app/tests/cases/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/console/templates/skel/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/console/templates/skel/tests/cases/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/libs/controller
/Applications/MAMP/htdocs/my_cake1.3_project/cake/tests/cases/libs/controller
/Applications/MAMP/htdocs/my_cake1.3_project/cake/tests/test_app/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/tests/test_app/plugins/test_plugin/controllers
/Applications/MAMP/htdocs/my_cake2_project/app/Controller
/Applications/MAMP/htdocs/my_cake2_project/app/Test/Case/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Console/Templates/skel/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Console/Templates/skel/Test/Case/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Test/Case/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Test/test_app/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Test/test_app/Plugin/TestPlugin/Controller

现在,有时我我想找到一段代码,我知道我在我的 CakePHP 项目的控制器中使用过,但我不记得它是哪个项目,所以我想搜索所有这些代码。不过,我不想浪费时间在“app/tests/cases/controllers”文件夹或“cake/”中的任何文件夹中搜索。 find /Applications/MAMP/htdocs -type d -iname Controller* | 查找 /Applications/MAMP/htdocs -type d -iname Controller* | grep -i /app/Controller 命令为我提供了要搜索的文件夹列表:

/Applications/MAMP/htdocs/my_cake1.3_project/app/controllers
/Applications/MAMP/htdocs/my_cake2_project/app/Controller

我只需要找到一种方法来获取该输出,在每个文件夹的末尾添加斜杠和星号 (/*)行,并将每一行通过管道传输到 grep -il "string to search for" 命令。任何帮助将不胜感激。谢谢!

I'm looking for help with a one-liner that I can run from the Mac OS X terminal. I use MAMP for web development on my Mac. I have a lot of CakePHP projects in my "/Applications/MAMP/htdocs" directory. For the sake of simplicity, let's just say that I had two CakePHP projects and that this was the output of the find /Applications/MAMP/htdocs -type d -iname Controller* command:

/Applications/MAMP/htdocs/my_cake1.3_project/app/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/app/tests/cases/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/console/templates/skel/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/console/templates/skel/tests/cases/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/libs/controller
/Applications/MAMP/htdocs/my_cake1.3_project/cake/tests/cases/libs/controller
/Applications/MAMP/htdocs/my_cake1.3_project/cake/tests/test_app/controllers
/Applications/MAMP/htdocs/my_cake1.3_project/cake/tests/test_app/plugins/test_plugin/controllers
/Applications/MAMP/htdocs/my_cake2_project/app/Controller
/Applications/MAMP/htdocs/my_cake2_project/app/Test/Case/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Console/Templates/skel/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Console/Templates/skel/Test/Case/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Test/Case/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Test/test_app/Controller
/Applications/MAMP/htdocs/my_cake2_project/lib/Cake/Test/test_app/Plugin/TestPlugin/Controller

Now, sometimes I want to find a piece of code that I know I used in one of my CakePHP projects' controllers, but I can't remember which project it was, so I want to search all of them. I don't want to waste time searching in the "app/tests/cases/controllers" folder or any of the ones within "cake/", though. The find /Applications/MAMP/htdocs -type d -iname Controller* | grep -i /app/Controller command gives me the list of folders I want to search in:

/Applications/MAMP/htdocs/my_cake1.3_project/app/controllers
/Applications/MAMP/htdocs/my_cake2_project/app/Controller

I just need to find a way to take that output, add a slash and asterisk (/*) to the end of each line, and pipe each line to the grep -il "string to search for" command. Any help would be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吝吻 2025-01-12 01:30:32

解决方案 1

也许您想检查 find 命令的两个选项:(i)pathregex
使用它们,您可以缩小查找结果范围,并将找到的文件传递到您的 grep -il "searchString" 例如通过 |xargs 。看起来像:

    find /Applications/MAMP/htdocs -type f -ipath "*/app/Controller/*.php" 
| xargs grep -il 'foo'  

使用 -regex 会更灵活。

解决方案 2

但是,如果您确实想要:

找到一种获取该输出的方法,将斜杠和星号 (/*) 添加到
每行末尾,并将每行通过管道传输到 grep -il "要搜索的字符串
for”命令。

(顺便说一句,这里“管道”不起作用。)

你可以这样做:

find .(your original find).. |grep -i "/app/Controller" 
 |sed -r 's#^(.*)$#grep -il "foo" \1/*#g'|sh  

这个技巧是由 sed....|sh 完成的。sed 行将选择结果在之前的 grep 中,添加 grep 命令和选项 :(grep -il "foo") 并附加“/*”以构建完整的 grep 命令和管道。 sh,执行它。

solution 1

maybe you want to check two options of find command: (i)path and regex
with them you could narrow your find result and pass found files to your grep -il "searchString" for example by |xargs . it looks like:

    find /Applications/MAMP/htdocs -type f -ipath "*/app/Controller/*.php" 
| xargs grep -il 'foo'  

with -regex would be more flexiable.

solution 2

however if you really really want to :

find a way to take that output, add a slash and asterisk (/*) to the
end of each line, and pipe each line to the grep -il "string to search
for" command.

(btw, here "pipe" won't work.)

you could do this:

find .(your original find).. |grep -i "/app/Controller" 
 |sed -r 's#^(.*)$#grep -il "foo" \1/*#g'|sh  

the trick was done by the sed....|sh. the sed line will pick the result of your previous grep, add grep command and options :(grep -il "foo") and append "/*" in order to construct a complete grep command. finally pipe to sh, to execute it.

唔猫 2025-01-12 01:30:32

你试过这个吗?

find /Applications/MAMP/htdocs -type d -iname Controller* 
-exec grep -il "string to search for" {} /;

Have you tried this?

find /Applications/MAMP/htdocs -type d -iname Controller* 
-exec grep -il "string to search for" {} /;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文