If you are determined to reimplement existing functionality, just keep a queue with the last 5 lines, and print those and the next 5 lines when you find a match.
In reply to Re^3: printing several lines around match
by moritz
in thread printing several lines around match
by auto_w
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |