How can I search for a multiline pattern in a file?

Oli picture Oli · Sep 30, 2008 · Viewed 127.3k times · Source

I needed to find all the files that contained a specific string pattern. The first solution that comes to mind is using find piped with xargs grep:

find . -iname '*.py' | xargs grep -e 'YOUR_PATTERN'

But if I need to find patterns that spans on more than one line, I'm stuck because vanilla grep can't find multiline patterns.

Answer

Amit picture Amit · Sep 15, 2010

Why don't you go for awk:

awk '/Start pattern/,/End pattern/' filename