如何通过 grep 查找位于许多不同目录中的代码?

发布于 2024-07-21 20:56:12 字数 125 浏览 5 评论 0原文

我正在开发一个大量使用 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 技术交流群。

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

发布评论

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

评论(12

悲欢浪云 2024-07-28 20:56:12
find DIRECTORY -name "*.py" | xargs grep PATTERN

顺便说一句,自从写这篇文章以来,我发现了 ack,这是一个更好的解决方案。

(自从那次编辑后,我发现了 ag)。

find DIRECTORY -name "*.py" | xargs grep PATTERN

By the way, since writing this, I have discovered ack, which is a much better solution.

(And since that edit, I have discovered ag).

过期以后 2024-07-28 20:56:12
grep -r -n "PATTERN" --include="*.py" DIRECTORY
grep -r -n "PATTERN" --include="*.py" DIRECTORY
忱杏 2024-07-28 20:56:12

我强烈推荐 ack,一个 grep 替代品,“针对拥有大量异构源代码树的程序员”(来自网站)

I would strongly recommend ack, a grep substitute, "aimed at programmers with large trees of heterogeneous source code" (from the website)

终陌 2024-07-28 20:56:12

这些天我也经常使用ack。 我确实对其进行了一些调整以找到所有相关的文件类型:

# Add zcml to the xml type:
--type-add
xml=.zcml

# Add more files the plone type:
--type-add
plone=.dtml,.zpt,.kss,.vpy,.props

# buildout config files
--type-set
buildout=.cfg

# Include our page templates to the html type so we can limit our search:
--type-add
html=.pt,.zpt

# Create txt file type:
--type-set
txt=.txt,.rst

# Define i18n file types:
--type-set
i18n=.pot,.po

# More options
--follow
--ignore-case
--nogroup

重要的是要记住,如果扩展名不在其配置中,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:

# Add zcml to the xml type:
--type-add
xml=.zcml

# Add more files the plone type:
--type-add
plone=.dtml,.zpt,.kss,.vpy,.props

# buildout config files
--type-set
buildout=.cfg

# Include our page templates to the html type so we can limit our search:
--type-add
html=.pt,.zpt

# Create txt file type:
--type-set
txt=.txt,.rst

# Define i18n file types:
--type-set
i18n=.pot,.po

# More options
--follow
--ignore-case
--nogroup

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?

握住我的手 2024-07-28 20:56:12

这个问题是创建 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.

木有鱼丸 2024-07-28 20:56:12
find <directory> -name '*.py' -exec grep <pattern> {} \;
find <directory> -name '*.py' -exec grep <pattern> {} \;
¢蛋碎的人ぎ生 2024-07-28 20:56:12

如果你想 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.

吃颗糖壮壮胆 2024-07-28 20:56:12

以防万一您想要非命令行 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.

一枫情书 2024-07-28 20:56:12

我推荐 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).

剑心龙吟 2024-07-28 20:56:12

仅仅因为没有足够的答案...

如果您经常进行开发,那么使用 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.

几味少女 2024-07-28 20:56:12

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/

烟酉 2024-07-28 20:56:12

自从发现 Emacs 的 rgrep 命令以来,我的 grep 生活更加令人满意。

假设我想在 Plone 的源代码中找到“IPortletDataProvider”。 我这样做:

  1. Mx rgrep
  2. Emacs 提示输入搜索字符串 (IPortletDataProvider)
  3. ...然后要搜索哪些文件 (*.py)
  4. ...然后是哪个目录 (~/Plone/buildout-cache/eggs )。 如果我已经在编辑文件,则默认为该文件的目录,这通常正是我想要的。

结果出现在新的缓冲区中。 顶部是find | xargs grep 命令 Emacs 运行。 所有匹配项都会突出显示。 我可以使用标准文本搜索命令搜索缓冲区。 最重要的是,我可以在匹配项上按 Enter 键(或单击)来打开该文件。

这是一种非常好的工作方式。 我喜欢我不必记住 find | xargs grep 参数序列,但如果我需要的话,所有的力量都在那里。


Emacs rgrep 示例

My grepping life is way more satisfying since discovering Emacs' rgrep command.

Say I want to find 'IPortletDataProvider' in Plone's source. I do:

  1. M-x rgrep
  2. Emacs prompts for the search string (IPortletDataProvider)
  3. ... then which files to search (*.py)
  4. ... then which directory (~/Plone/buildout-cache/eggs). If I'm already editing a file, this defaults to that file's directory, which is usually exactly what I want.

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.


Emacs rgrep example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文