http://www.perlmonks.org?node_id=788550


in reply to How can I print three lines before pattern match?

Do you have to use Perl?
Of course it has to be Perl! ;-)

Otherwise grep will do it:

grep -B3 pattern logfile