[SunHELP] grep

Wyatt Draggoo wyatt at draggoo.com
Thu Jul 31 11:40:26 CDT 2003


I couldn't find a way to do it with just grep, but you could do something
like this:

# for i in `grep -n pattern filename | awk -F: '{print $1}'`; do
>    perl -ne "print if ($i-2)..$i" filename
>    echo
> done

Change the ($i-2) and $i values in the Perl line to get different numbers
of surrounding lines.  ($i-1)..($i+1) would print the line before and the
line after the matchine line.  Note that if two lines in a row match, some
lines will be duplicated in the output.

Wyatt

> 
> Hey Everybody:
> 
> I was trying to find a way to grep a pattern in a file and then print out 
> that line and the two lines preceding the match.
> I already manned the grep, egrep and fgrep man pages and nothing was
listed 
> that fit the bill.  Maybe there is a more robust version
> of grep out there.  Any pointers would be appreciated.  Thanks in advance.
> 
> Bret
> _______________________________________________
> SunHELP maillist  -  SunHELP at sunhelp.org
> http://www.sunhelp.org/mailman/listinfo/sunhelp

--
Wyatt Draggoo



More information about the SunHELP mailing list