UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 13th, 2006, 06:57 PM
timely timely is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: Dallas, TX
Posts: 1 timely User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 32 sec
Reputation Power: 0
Question Getting line surrounding your grep keyword

I am looking to create a script where I can do a search on a keyword and not only get the lines it shows up on but 3 lines above and below its. I am using HP-Unix. Grep on my machine does not have -A or -B. It may have sever occurance of the same search. I also plan to it to a temp file that I can read out of.

Thanks,

Reply With Quote
  #2  
Old August 14th, 2006, 03:31 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 759 SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 20 h 48 m 37 sec
Reputation Power: 336
I have had to do something similar - what I did was to use the -n option (I think it was) of grep to return the line number, then use that (suitably added/subtracted from) with sed. I only had a small file so that was ok for me. If you have a big file you may wish to use another method.
awk may be of use - keep a rolling track of current line - 3, -2, and -1 and when you find your keyword print them, and following 3 lines out.

Reply With Quote
  #3  
Old August 15th, 2006, 09:07 AM
Verletto Verletto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Location: Sweden
Posts: 14 Verletto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 56 m 46 sec
Reputation Power: 0
A barbarian way of doing this could be to write each line of your input into a work file, and grep -q on each one read this way. If the grep is successful, you can print the tail -3 of the workfile.

Not as quick as an awk call I´m afraid. Well, I said it was barbarian.

Reply With Quote
  #4  
Old August 20th, 2006, 01:59 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 17 m 10 sec
Reputation Power: 48
Not sure I understood what you wanted but
Code:
sed -n -e '/Bad/{=;1!p;g;$!N;p;D;}' -e h  filename


on this data:
Code:
hello 
hi 
*User 
Bad 
*Morning 
Good 
Bye
finds the line bad at line 4 and displays line 3,4,5
Code:
4
Bad 
*User 
*Morning 

Reply With Quote
  #5  
Old August 20th, 2006, 02:03 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 17 m 10 sec
Reputation Power: 48
Thinking about, changed my mind. Easy to change small things
Using the example file above:
Code:
grep -A1 -B1 'Bad' filename

seems to be what you're asking for...

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Getting line surrounding your grep keyword


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT