The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem in the return of grep mail2sant Shell Programming and Scripting 1 04-11-2008 04:00 AM
return the previous line user_prady Shell Programming and Scripting 12 12-18-2007 04:37 AM
using Lynx and Grep to return search page rank - help roninuta UNIX for Dummies Questions & Answers 0 09-18-2007 09:27 AM
how to get return code in one line bluemoon1 Shell Programming and Scripting 6 09-13-2007 07:09 PM
modifying grep to return latest files ragha81 Shell Programming and Scripting 4 12-28-2006 01:09 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-05-2007
Registered User
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
grep for a line then return lines above

Hey guys,

I just want to grep for a line then return a few lines above it I can't seem to find what im looking for on google can someone help me out?

This is on Solaris 9.....

I don't have GNU grep so -B and -A commands will not work

Last edited by kingdbag; 03-05-2007 at 03:01 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 03-05-2007
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Grab GNU grep from sunfreeware.com

http://www.sunfreeware.com/programlistsparc9.html#grep

You can try something like this although it's convoluted.

Cheers
ZB
Reply With Quote
  #3  
Old 03-05-2007
Registered User
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
Wow, do i really need to script something for grep to do this?

I found this and its supposed to return the above 3 lines when it finds FORWARD in the text file and it is blank...

sed -n '/FORWARD/{g;3!p;};h' crap.txt

Last edited by kingdbag; 03-05-2007 at 03:42 PM.
Reply With Quote
  #4  
Old 03-05-2007
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 784
Quote:
Originally Posted by kingdbag
Hey guys,

I just want to grep for a line then return a few lines above it I can't seem to find what im looking for on google can someone help me out?

This is on Solaris 9.....

I don't have GNU grep so -B and -A commands will not work
Code:
pattern=$1
num=$2
shift
nawk -v pattern="$pattern" -v num="$num" '
                       { x[NR % num] = $0 }
  index( $0, pattern ) {
                 n = NR % num
                 top = n + num
                 while ( n++ < top ) print x[n % num]
               }'
I have used nawk, since the default Solaris awk is a version which has been obsolete for almost 20 years.
More recent versions on Solaris, include nawk and /usr/xpg4/bin/awk.

The above version only handles strings, not regular expressions. The following one deals with regexps, but will duplicate lines if there is an overlap. (I may fix that if I have a moment or to, or perhaps someone else can do it.)

Code:
pattern=$1
num=$2
shift
nawk -v num=$num '
                       { x[NR % num] = $0 }
  /'"$pattern"'/ {
                 n = NR % num
                 top = n + num
                 while ( n++ < top ) print x[n % num]
               }'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0