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
Grep question. saurabhsinha23 UNIX for Dummies Questions & Answers 3 12-10-2007 09:58 PM
grep question tim-bobby UNIX for Dummies Questions & Answers 4 10-05-2007 08:58 AM
Question about GREP Adriel UNIX for Dummies Questions & Answers 16 03-22-2007 04:03 AM
grep & sed question der Kopf Shell Programming and Scripting 1 11-22-2004 12:49 AM
Grep question eloquent99 UNIX for Dummies Questions & Answers 4 02-20-2003 06:49 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-26-2006
Registered User
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
Another grep question

Say I want to grep for a line with EXEC in it

grep EXEC blah.conf

thats cool but what if i want to grep for EXEC and if it finds a line with that in it to also grab the two lines above it?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-26-2006
Registered User
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 395
Depends on the OS. On my mac

Code:
$ grep -B 2 EXEC testfile
this is a test line 3
this is a test line 4
EXEC
--
this is a test line 7
this is a test line 8
EXEC
--
this is a test line 13
this is a test line 14
EXEC
Same with Linux (Mandrake was the test box).

Carl
Reply With Quote
  #3 (permalink)  
Old 10-26-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,325
That won't work anywhere else. You will have to probably use a combo of 'grep -n' and then sed to print the lines before your match. You end up scanning the input file too many times which makes it quite slow.
Reply With Quote
  #4 (permalink)  
Old 10-26-2006
Registered User
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 395
Yep, that's what happens when you don't offer the OS you're talking about It's also why I'll typically note which OS I'm offering the answer for. Technically you should be able to get GNU/grep for the major OSs' (like sunfreeware.com).

My Sun boxes aren't up and I don't want to VPN to work and check out the HP-UX or AIX boxes I have available to me for this question. If it's still up when I get back on-line to work tomorrow, I'll check them as well.

Carl
Reply With Quote
  #5 (permalink)  
Old 10-26-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,243
If you don't have GNU grep...
Code:
[n]awk '{a[NR%p]=$0}$0~s{for(c=p;c>0;c--)print a[(NR-c+1)%p]}' p=3 s=EXEC blah.conf
Reply With Quote
  #6 (permalink)  
Old 10-26-2006
Registered User
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
Oh, I'm using Solaris 9 sorry about that!
Reply With Quote
  #7 (permalink)  
Old 10-26-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,543
Quote:
Originally Posted by kingdbag
Say I want to grep for a line with EXEC in it

grep EXEC blah.conf

thats cool but what if i want to grep for EXEC and if it finds a line with that in it to also grab the two lines above it?
Python alternative:
Sample input:
abc
def
ghi
EXEC
klm
nop
qrs
tuv
wxy


Code:
#!/usr/bin/python
data = open("input.txt").readlines()
for linenum , line in enumerate(data):
 	if 'EXEC' in line:
 		print ''.join(data[linenum - 2 : linenum])
output:
Code:
def
ghi
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:06 AM.


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 Global Fact Book

Content Relevant URLs by vBSEO 3.2.0