Another grep question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Another grep question
# 1  
Old 10-26-2006
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  
Old 10-26-2006
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  
Old 10-26-2006
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  
Old 10-26-2006
Yep, that's what happens when you don't offer the OS you're talking about Smilie 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  
Old 10-27-2006
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  
Old 10-27-2006
Oh, I'm using Solaris 9 sorry about that!
# 7  
Old 10-27-2006
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep Question

My grep returns a row of data like this: 75=20130130;60=074338;61=985;511=55473883;452=115439;62=196;267=1; Is there a way for the grep to only return 60="something" and 511="something" ? Thanks in advance. (10 Replies)
Discussion started by: Carl2013
10 Replies

2. UNIX for Dummies Questions & Answers

Question on grep

Hello all, I'm trying to grep the string "scott" from all files whose names are like srvr*.log and that were created "Nov 15"...I'm trying the following command but throws an error message...seems like the syntax is incorrect.. grep scott < ls -l srvr*.log|grep "Nov 15" Thanks for your... (9 Replies)
Discussion started by: luft
9 Replies

3. Shell Programming and Scripting

Question about grep

is there anyway i can ask grep to only get the first line? as in the top command line line 1 <-- just grep this line line 2 line 3 ---------- Post updated at 04:24 PM ---------- Previous update was at 04:19 PM ---------- nvm.. found out that i can do it with |head (12 Replies)
Discussion started by: Nick1097
12 Replies

4. Shell Programming and Scripting

Question about grep

can anyone tell me what the \/$ means? from grep \/$ (8 Replies)
Discussion started by: Nick1097
8 Replies

5. Shell Programming and Scripting

grep question please

i have files with "DOMAINSOLVER ACMS" with any number of spaces in between the two words on its own line and i can find it with the following: grep -c "DOMAINSOLVER* ACMS" $FILENAMEbut i need to exclude any lines matching: "$DOMAINSOLVER". i've tried a variety of quoting and escaping with no luck.... (4 Replies)
Discussion started by: crimso
4 Replies

6. Shell Programming and Scripting

grep question

Hello, Is there a way in grep to remember patterns? For eg: int a,b,c,d,a; If a variable is declared twice, like in the previous example, I should be able to print only those lines. Is there a way to print only the lines where the variable name occurs more than once, using grep... (1 Reply)
Discussion started by: prasanna1157
1 Replies

7. UNIX for Dummies Questions & Answers

grep question

Instead of using the following command #dmesg | grep -v sendmail | grep -v xntpd How can I use just one grep -v and give both arguments. Please suggest thanks (4 Replies)
Discussion started by: Tirmazi
4 Replies

8. Shell Programming and Scripting

grep question

hello people, All my servers have 4 mounts with this norme. For example, if my hostname is siroe. df -h | grep `hostname` /dev/dsk/c1t3d0s6 404G 399G 800M 100% /siroe3 /dev/dsk/c1t2d0s6 404G 399G 800M 100% /siroe2 /dev/md/dsk/d6 20G 812M 19G ... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

9. UNIX for Dummies Questions & Answers

Grep Question

Hello Everybody, I have files; yyyymmdd.log which the data look like this; "Txid=9426043&MsgTxt=Thankyou&UserId=john&Password=jh2501" "Txid=9426150&MsgTxt=Thankyou&UserId=john&Password=jh2501" . . . "Txid=9426200&MsgTxt=Thankyou&UserId=john&Password=jh2501" Question 1: How to... (3 Replies)
Discussion started by: nazri76
3 Replies

10. UNIX for Dummies Questions & Answers

grep question

what is the format for grep if I want to search from the current directory and through all its subdirectories?:) (3 Replies)
Discussion started by: pkappaz
3 Replies
Login or Register to Ask a Question