LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep -B using sed (https://www.linuxquestions.org/questions/programming-9/grep-b-using-sed-661964/)

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

Quote:

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

http://sed.sourceforge.net/grabbag/scripts/cgrep.sh.txt

w3bd3vil 08-11-2008 08:12 AM

I found this also to be quite useful.

Quote:

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 http://www.sunfreeware.com/


All times are GMT -5. The time now is 06:57 PM.