简体   繁体   中英

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. 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.*$)/

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.

So you might want to look for other options than just grep .

grep manual page for more info.

You can use pcgregrep :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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