printing a block of text that matches a pattern
awk '
{
if (/-------------------------------------------------/) {
if (hold ~ /INFO/) {
print hold;
}
hold="";
} else {
hold=hold "\n" $0
}
}
END {
if (hold ~ /INFO/) {
print hold;
}
}' file
This uses a 'hold'ing variable (ala sed) to accumulate lines between separated blocks; once a new block (or EOF) is encountered, print the held value only if it matches the /INFO/ pattern.
(re: the older comments, I deleted my previous inadequate awk and perl answers to clean up this answer)
Should be quite easy with awk
if you don't need all -
in the output:
awk -vRS='----' '/banana/{print}' file
alternatively pcregrep
:
pcregrep -M '^-+[^-]*banana[^-]*' file
If you don't mind the missing leading empty lines, here's a sed
solution:
sed '/---/b end # if line matches pattern branch to : end
//!{H # if it doesn't match, append to hold space
$!d # and if not on the last line, delete it
$b end # if it's the last line branch to : end
}
: end # label end
x # exchange hold buffer and pattern space
/PATTERN/!d # if pattern space doesn't match, delete it
' <infile