![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | 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? |
| Forum Sponsor | ||
|
|
|
|||
|
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
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 |
|||
| Google The UNIX and Linux Forums |