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.
So I discovered pcregrep which stands for Perl Compatible Regular Expressions GREP.
For example, you need to find files where the '_name' variable is immediatelly followed by the '_description' variable:
Tip: you need to include the line break character in your pattern. Depending on your platform, it could be '\n', \r', '\r\n', ...
Why don't you go for awk:
grep -P
also uses libpcre, but is much more widely installed. To find a completetitle
section of an html document, even if it spans multiple lines, you can use this:Since the PCRE project implements to the perl standard, use the perl documentation for reference:
@Marcin: awk example non-greedy:
With silver searcher:
Speed optimizations of silver searcher could possibly shine here.
You can use the grep alternative sift here (disclaimer: I am the author).
It support multiline matching and limiting the search to specific file types out of the box:
(search all *.py files for the specified multiline regex pattern)
It is available for all major operating systems. Take a look at the samples page to see how it can be used to to extract multiline values from an XML file.