Grep 正则表达式以任意顺序查找单词

发布于 2024-11-05 17:21:27 字数 151 浏览 0 评论 0原文

上下文:我想在很多源代码文件中找到一个类定义,但我不知道确切的名称。

问题:我知道一些单词必须出现在我想要查找的行上,但我不知道它们出现的顺序。有没有一种快速的方法可以在同一行上以任意顺序查找多个单词?

Context: I want to find a class definition within a lot of source code files, but I do not know the exact name.

Question: I know a number of words which must appear on the line I want to find, but I do not know the order in which they will appear. Is there a quick way to look for a number of words in any order on the same line?

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

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

发布评论

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

评论(5

寻找一个思念的角度 2024-11-12 17:21:27

对于需要搜索大量单词的情况,可以使用 awk,如下所示:(

awk "/word1/&&/word2/&&/word3/" *.c

如果您是 cygwin 用户,则命令为 gawk。)

For situations where you need to search on a large number of words, you can use awk as follows:

awk "/word1/&&/word2/&&/word3/" *.c

(If you are a cygwin user, the command is gawk.)

疯狂的代价 2024-11-12 17:21:27

如果您想查找 foo、bar 和 baz,您可以这样做:

grep foo *.c | grep bar | grep baz

这将找到以任意顺序包含这三个元素的任何内容。如果使用egrep,则可以使用单词边界,否则将匹配子字符串。

If you're trying to find foo, bar, and baz, you can just do:

grep foo *.c | grep bar | grep baz

That will find anything that has all three in any order. You can use word boundaries if you use egrep, otherwise that will match substrings.

一抹淡然 2024-11-12 17:21:27

虽然这不是您的 grep 问题的确切答案,但您应该检查“ctags”命令以从源代码生成标签文件。对于源代码对象,这应该比简单的 grep 对您有更多帮助。检查:http://ctags.sourceforge.net/ctags.html

While this is not an exact answer your grep question, but you should check the "ctags" command for generating tags file from the source code. For the source code objects this should help you a much more than an simple grep. check: http://ctags.sourceforge.net/ctags.html

忆伤 2024-11-12 17:21:27

使用标准基本正则表达式从当前目录开始递归匹配任何带有指定单词的 .c 文件(不区分大小写,bash 风格):

grep -r -i 'word1\|word2\|word3' ./*.c

使用标准扩展正则表达式:

grep -r -i -E 'word1|word2|word3' ./*.c

您还可以使用正则表达式:

grep -r -i -P 'word1|word2|word3' ./*.c

Using standard basic regex recursively match starting from the current directory any .c file with the indicated words (case insesitive, bash flavour):

grep -r -i 'word1\|word2\|word3' ./*.c

Using standard extended regex:

grep -r -i -E 'word1|word2|word3' ./*.c

You can also use perl regex:

grep -r -i -P 'word1|word2|word3' ./*.c
江城子 2024-11-12 17:21:27

如果您需要使用单个 grep 命令进行搜索(例如,您正在标准输入上搜索多个模式替代方案),您可以使用:

grep -e 'word1.*word2' -e 'word2.*word1' -e 'alternative-word'

这将找到包含 word1word2 任意顺序, 替代词

(请注意,随着任意顺序的单词数量增加,此方法会变得指数级复杂。)

If you need to search with a single grep command (for example, you are searching for multiple pattern alternatives on stdin), you could use:

grep -e 'word1.*word2' -e 'word2.*word1' -e 'alternative-word'

This would find anything which has word1 and word2 in either order, or alternative-word.

(Note that this method gets exponentially complicated as the number of words in arbitrary order increases.)

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