in reply to Equivalent of Linux Grep Command

Even if you don't want to use ack from the command line, take a look at how it handles the -A and -B flags. It's not very complex.

xoxo,
Andy