Here's a simpler approach using Perl:
perl -e '$f=join("",<>); print $& if $f=~/foo\nbar.*\n/m' file
or (since JosephR took the
sed route, I'll shamelessly steal his suggestion)
perl -n000e 'print $& while /^foo.*\nbar.*\n/mg' file
$f=join("",<>); : this reads the entire file and saves it's contents (newlines and all) into the variable
$f. We then attempt to match
foo\nbar.*\n, and print it if it matches (the special variable
$& holds the last match found). The
///m is needed to make the regular expression match across newlines.
-0 sets the input record separator. Setting this to
00 activates 'paragraph mode' where Perl will use consecutive newlines (
\n\n) as the record separator. In cases where there are no consecutive newlines, the entire file is read (slurped) at once.
Do not do this for large files, it will load the entire file into memory and that may be a problem.