简体   繁体   English

什么是匹配线的 grep 模式?

[英]What is a grep pattern for matching lines?

I am trying to find all cases in my test suite where one-line it methods are used back to back.我试图在我的测试套件中找到所有使用单行it方法的案例。 Basically, I'm looking for files that have lines like these:基本上,我正在寻找具有以下行的文件:

it { expect(some_stuff).to be_true }
it { expect(other_stuff).to be_false }

but not this:但不是这个:

it { expect(some_stuff).to be_true }
# a bunch of other stuff
it { expect(other_stuff).to be_true }

and can't quite figure out the pattern.并且无法完全弄清楚模式。 I have tried:我努力了:

> grep it.\{*\}\n*it ./test/directory -lR
# and
> grep it.\{*\}\n*it.\{*

which returns nothing, but I know there is at least one matching file.它什么都不返回,但我知道至少有一个匹配的文件。 Does anyone know how to create the pattern I'm looking for?有谁知道如何创建我正在寻找的模式?

The pattern you might be looking for is /(?:it.*$)\n(?:it.*$)/您可能正在寻找的模式是/(?:it.*$)\n(?:it.*$)/

But the problem you might be facing with grep or egrep is that this utility searches through a file single line at a time, so it doesn't support let say \n in a regular expression.但是您可能在使用grepegrep时遇到的问题是,此实用程序一次只搜索一行文件,因此它不支持在正则表达式中使用\n

So you might want to look for other options than just grep .因此,您可能想寻找除grep之外的其他选项。

grep manual page for more info. grep手册页了解更多信息。

You can use pcgregrep :您可以使用pcgregrep

pcregrep -lr -M "it.{.*}\nit" ./test/directory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM