LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-11-2008, 07:31 AM   #1
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189

Rep: Reputation: 49
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?
 
Old 08-11-2008, 08:07 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
http://sed.sourceforge.net/grabbag/scripts/cgrep.sh.txt
 
Old 08-11-2008, 09:12 AM   #3
w3bd3vil
Senior Member
 
Registered: Jun 2006
Location: Hyderabad, India
Distribution: Fedora
Posts: 1,189

Original Poster
Rep: Reputation: 49
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.
 
Old 08-11-2008, 07:37 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Ask your sysadmin, but there are Sun pkgs that include the GNU tools, see http://www.sunfreeware.com/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use grep/sed tnik Linux - General 3 11-06-2007 08:51 PM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 01:34 AM
I need some help with SED and/or GREP... TheEngineer Linux - Newbie 1 02-02-2007 09:17 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 07:30 AM


All times are GMT -5. The time now is 02:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration