cgrep — a grep enhancement to show preceding lines
Solaris grep has no way to printing the lines surrounding a match. Yet my most common use of grep is to locate particular strings in a log file, with the goal of looking at the log messages previous to them. There is a simple solution for this. And also, please note, I did not write any of the code linked here. Just posting it to help others find it.
cgrep shell script: http://examples.oreilly.com/upt2/split/cgrep.sed
#!/bin/sh
# cgrep - context grep via sed
# Written by Greg Ubben, DoD, 10 Dec 1993
#
# Cgrep finds all occurrences of <pattern> in <file...>, showing <context>
# additional lines of context above and below each occurrence. <context>
# must be at least 1, and defaults to 2. Each new block of context is
# preceded by the line number of the first occurrence within that block.
# <pattern> must be a grep-style regular expression, except that you can
# match across lines, so use "\nWORD" vs "^WORD" and "WORD\n" vs "WORD$".
#
# The following example will find all occurrences of the word "five" where
# it is followed by the word "seven" somewhere within the next 3 lines:
# cgrep -3 "five.*seven"
USAGE="$0 [-context] pattern [file...]"
n=3
case $1 in -[1-9]*)
n=`expr 1 - "$1"`
shift
esac
re=${1?}; shift
sed -n "
1b start
: top
\~$re~{
h; n; p; H; g
b endif
}
N
: start
//{ =; p; }
: endif
$n,\$D
b top
" "$@"
This is extra useful for production systems where you can't install packages or make changes to /bin. Just copy the script into your home directory, then either edit your PATH to include your home directory, or reference this directly.