The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-26-2006
kingdbag kingdbag is offline
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?
  #2 (permalink)  
Old 10-26-2006
BOFH BOFH is offline Forum Advisor  
Registered User
  
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 406
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
  #3 (permalink)  
Old 10-26-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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.
  #4 (permalink)  
Old 10-26-2006
BOFH BOFH is offline Forum Advisor  
Registered User
  
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 406
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
  #5 (permalink)  
Old 10-26-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,402
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
  #6 (permalink)  
Old 10-27-2006
kingdbag kingdbag is offline
Registered User
  
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
Oh, I'm using Solaris 9 sorry about that!
  #7 (permalink)  
Old 10-27-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
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
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, linux mandrake

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0