我正在寻找可以从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* | 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"
命令。任何帮助,将不胜感激。谢谢!
答案 0 :(得分:0)
find /Applications/MAMP/htdocs -type d -iname Controller*
-exec grep -il "string to search for" {} /;
答案 1 :(得分:0)
解决方案1
也许你想检查一下find命令的两个选项:(i)path
和regex
使用它们,您可以缩小查找结果并将找到的文件传递到grep -il "searchString"
,例如|xargs
。它看起来像:
find /Applications/MAMP/htdocs -type f -ipath "*/app/Controller/*.php"
| xargs grep -il 'foo'
使用-regex
的会更灵活。
解决方案2
但是如果你真的想要:
找到一种方法来获取该输出,添加斜杠和星号(/ *) 每行的结尾,并将每一行传递给grep -il“要搜索的字符串 为“命令。
(顺便说一句,这里“管道”不起作用。)
你可以这样做:
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,执行它。