![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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? |
|
||||
|
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 Carl |
|
||||
|
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 |
|
||||
|
Quote:
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])
Code:
def ghi |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| linux, linux mandrake |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|