-   Programming (
-   -   grep -B using sed (

w3bd3vil 08-11-2008 06:31 AM

grep -B using sed
I was looking for a way to print 10 lines above a regexp
grep -B works
but it doesnt on my SunOS's
I was looking for a sed alternative or an awk or maybe perl


sed -n '/regexp/{g;1!p;};h' filename.txt
The above code just prints 1 line above the regexp. How do I print 10 lines or maybe 15?

burschik 08-11-2008 07:07 AM

w3bd3vil 08-11-2008 08:12 AM

I found this also to be quite useful.


nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="string" file1
a,b,s are the variables to change.

chrism01 08-11-2008 06:37 PM

Ask your sysadmin, but there are Sun pkgs that include the GNU tools, see

All times are GMT -5. The time now is 03:46 PM.