Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Solaris would be preferable, but I'll take linux or something I can compile. Does such a beast exist?

share|improve this question
up vote 6 down vote accepted

You can get GNU grep and it's dependencies for Solaris from either as a binary download in pkg format which installs in /usr/local/bin or as a source package.

share|improve this answer

As far as GNU grep go, this will show you the number of lines before the match:

# grep -B number

Equivilent for after:

# grep -A number

You can download GNU Grep here:

share|improve this answer
And -C combines both A and B in one flag. – Daenyth Oct 20 '11 at 19:15

A small awk script will also work:

#!/usr/bin/awk -f
BEGIN { context=3; }
{ add_buffer($0) }
/pattern/ { print_buffer() }
function add_buffer(line)
    buffer[NR % context]=line
function print_buffer()
    for(i = max(1, NR-context+1); i <= NR; i++) {
        print buffer[i % context]
function max(a,b)
    if (a > b) { return a } else { return b }

replace /pattern/ with the actual regular expression or pattern to search for.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.