如何通过 grep 查找位于许多不同目录中的代码?
我正在开发一个大量使用 Egg 的 Python 程序(Plone)。 这意味着我在调试时可能想要搜索 198 个充满 Python 代码的目录。 有没有一种好方法只搜索这些目录中的 .py 文件,避免不相关的代码和大型二进制文件?
I'm working on a Python program that makes heavy use of eggs (Plone). That means there are 198 directories full of Python code I might want to search through while debugging. Is there a good way to search only the .py files in only those directories, avoiding unrelated code and large binary files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
顺便说一句,自从写这篇文章以来,我发现了 ack,这是一个更好的解决方案。
(自从那次编辑后,我发现了 ag)。
By the way, since writing this, I have discovered ack, which is a much better solution.
(And since that edit, I have discovered ag).
我强烈推荐 ack,一个 grep 替代品,“针对拥有大量异构源代码树的程序员”(来自网站)
I would strongly recommend ack, a grep substitute, "aimed at programmers with large trees of heterogeneous source code" (from the website)
这些天我也经常使用ack。 我确实对其进行了一些调整以找到所有相关的文件类型:
重要的是要记住,如果扩展名不在其配置中,ack 将找不到文件。 有关所有可用类型,请参阅“ack --help-types”。
我还假设您正在使用 omelette 这样您就可以 grep/确认/找到所有相关文件?
I also use ack a lot these days. I did tweak it a bit to find all the relevant file types:
Important to remember is that ack won't find files if the extension isn't in its configuration. See "ack --help-types" for all the available types.
I also assume you are using omelette so you can grep/ack/find all the related files?
这个问题是创建 Collective.recipe.omelette 的动机。 这是一种构建方法,可以将工作集中的所有鸡蛋符号链接到一个目录结构中,您可以将您最喜欢的搜索实用程序指向该目录结构。
This problem was the motivation for the creation of collective.recipe.omelette. It is a buildout recipe which can symlink all the eggs from your working set into one directory structure, which you can point your favorite search utility at.
如果你想 grep 中的标识符,还有 GNU idutils非常非常快的大型源代码树。 它需要通过运行 mkid (并调整其配置文件以不忽略 .py 文件)来提前构建搜索数据库。 如果您使用构建, z3c.recipe.tag 会处理这个问题。
There's also GNU idutils if you want to grep for identifiers in a large source tree very very quickly. It requires building a search database in advance, by running mkid (and tweaking its config file to not ignore .py files). z3c.recipe.tag takes care of that, if you use buildout.
以防万一您想要非命令行 OSS 解决方案......
我使用 pycharm。 它内置了对扩建的支持。 您将其指向构建生成的 bin/实例,并将项目的外部依赖项设置为实例使用的所有 Egg。 然后,所有 IDE 的自省和代码导航都可以正常工作。 Goto 定义、goto 实例、重构支持,当然还有搜索。
Just in case you want a non-commandline OSS solution...
I use pycharm. It has built in support for buildout. You point it at a buildout generated bin/instance and it sets the projects external dependencies to all the eggs used by the instance. Then all the IDE's introspection and code navigation work nicely. Goto definition, goto instances, refactoring support and of course search.
我推荐 grin 进行搜索,omelette 使用 plone 和 pydev 功能“全局浏览器”(使用 eclipse 或 aptana studio)时。
I recomend grin to search, omelette when working with plone and the pydev-feature 'Globals browser' (with eclipse or aptana studio).
仅仅因为没有足够的答案...
如果您经常进行开发,那么使用 Pydev 安装 Eclipse 是非常值得的(或者更简单,Aptana Studio - 这是一个修改后的 Eclipse),在这种情况下,查找工具是在那里。
And simply because there are not enough answers...
If you're developing routinely, it's well worth the effort to install Eclipse with Pydev (or even easier, Aptana Studio - which is a modified Eclipse), in which case the find tools are right there.
OpenGrok 是源搜索和导航的绝佳选择。 但在 Java 上运行。
我真的希望有类似的东西 https://oracle.github.io/opengrok/
OpenGrok is an excellent choice for source searching and navigation. Runs on Java, though.
I really wish there was something like https://oracle.github.io/opengrok/
自从发现 Emacs 的 rgrep 命令以来,我的 grep 生活更加令人满意。
假设我想在 Plone 的源代码中找到“IPortletDataProvider”。 我这样做:
Mx rgrep
结果出现在新的缓冲区中。 顶部是
find | xargs grep
命令 Emacs 运行。 所有匹配项都会突出显示。 我可以使用标准文本搜索命令搜索缓冲区。 最重要的是,我可以在匹配项上按 Enter 键(或单击)来打开该文件。这是一种非常好的工作方式。 我喜欢我不必记住
find | xargs grep
参数序列,但如果我需要的话,所有的力量都在那里。My grepping life is way more satisfying since discovering Emacs' rgrep command.
Say I want to find 'IPortletDataProvider' in Plone's source. I do:
M-x rgrep
The results appear in a new buffer. At the top is the
find | xargs grep
command Emacs ran. All matches are highlighted. I can search the buffer using the standard text search commands. Best of all, I can hit Enter (or click) on a match to open that file.It's a pretty nice way to work. I like that I don't have to remember
find | xargs grep
argument sequences, but that all that power is there if I need it.