Vlad Rudskoy
Vlad Rudskoy

Reputation: 716

Cannot understand regex with brackets

How to grep by regexp for files with helloworld(...) when it only contains rows with '.get' extension?

a(text)

b(text)

helloworld(
text.get
text.get
text.get
text.get
)
c(text)

Then, example like this:

a(text)

b(text)

helloworld(
text.pass
text.pass
text.pass
text.pass
)
c(text)

shouldn't be found

I tried regexp [a-z_]+\.get, but it helps to find files with extension, but I don't know how to wrap it with helloworld( and end with )

Upvotes: 2

Views: 65

Answers (2)

anubhava
anubhava

Reputation: 785196

If you have gnu grep then you can use -z option to treat while file as sequence of lines and use this command to list matching blocks:

grep -zoP 'helloworld\s*\(\s*(\w+\.get\b\s*)+\)\s*' file

To list all matching files in a directory use:

grep -zPl 'helloworld\s*\(\s*(\w+\.get\b\s*)+\)\s*' file*

Upvotes: 2

Toto
Toto

Reputation: 91430

AKAIK grep works one line at a time.

This perl one-liner does what you want:

perl -0777 -ne 'print $& if /helloworld\(\R(?:[a-z_]+\.get\R)+\)/' file

Result for first file:

helloworld(
text.get
text.get
text.get
text.get
)

Result for second file:

NOTHING

Upvotes: 1

Related Questions