Note you're output is not only showing 2 lines before the pattern, but also 1 line AFTER the pattern.
You don't need Perl for something that simple:
grep -B2 -A1 jack test.txt
UPDATE: Since the OP updated the original question, this approach is no longer valid. See my reply (Re^3: get n lines before a pattern) below.
In reply to Re: get n lines before a pattern
by VinsWorldcom
in thread get n lines before or after a pattern
by darklord_999
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |