Grep multiline pattern
Solution 1:
You could install pcregrep
(available in most distro repositories) - which is grep using the pcre library, which does "Perl Compatible Regular Expressions". It has a command line option -M
which allows you to do multiline searches - from the man page:
"The output for any one match may consist of more than one line."
So you could do
pcregrep -M 'my\s+ice\s+tea' filename
The \s
is whitespace, which will match \n
and \r
in multiline mode, in addition to the normal whitespace characters. You can also match the newline character directly, so you could do
pcregrep -M 'pattern1_\n_pattern2' filename
Solution 2:
I would probably do a search using vim
's :vimgrep
command. This works in a manner vaguely similar to that of grep
but supports vim REs and paths.
Basically you run something like :vimgrep 'pattern1\npattern2' path/**
for a recursive search, then type :copen
to bring up a smaller window containing a list of matches.
vim
REs can do mostly everything that PCREs can, but they evolved separately from the perl regular expression lineage so most of the advanced stuff works differently. Their basic functionality is more like that of basic REs, but they have some nifty additions that PCREs don't offer.
I'm not sure if it's possible to get :vimgrep
to spit out data as grep
does; I've only ever tried to use it for navigation within vim
.
:help vimgrep
from within vim
for more info; :help pattern.txt
for info on vim
REs; for more info on paths see :help wildcards
.