1753750 Members
5167 Online
108799 Solutions
New Discussion юеВ

Grep command

 
SOLVED
Go to solution
tom quach_1
Super Advisor

Grep command

Hello,

is there a way to grep this string "tw008070432"
in the file ltdbgout32700 as below and then print out that line and 5 line a bove and 5 line below that result.

$grep -in tw008070432 ltdbgout32700
189596:0x77ff1678: 44203D20 27545730 30383037 30343332 |D = 'TW008070432|
189601:e from DIST_G where DIST_ID = 'TW008070432' -0


Regards,
Tom
5 REPLIES 5
Pete Randall
Outstanding Contributor

Re: Grep command

Tom,

I've been playing with this bit of sed code (from "Handy One-Liners for SED" - attached) for the last few minutes, trying to get it to print more than just one line before and after:

# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (similar to "grep -A1 -B1")
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

I've also tried playing with grep, but can't find any sort of -A1 -B1 syntax.

Maybe you can figure something out that I'm not seeing.


Pete

Pete
Sandman!
Honored Contributor
Solution

Re: Grep command

How about using ex(1) for this task:

# ex -s +'/tw008070432/-5,/tw008070432/+5 p | q!' ltdbgout32700

~cheers
Michael Steele_2
Honored Contributor

Re: Grep command

Hey Sandman, that's cool. I had to take the single quotes out.

# ex -s +/Manager/-5,/Manager/+5 p | q!'file
Support Fatherhood - Stop Family Law
James R. Ferguson
Acclaimed Contributor

Re: Grep command

Hi Tom:

Your question is not infrequent. See this thread for some solutions. You can easily adopt mine or Hein's to process +-5 lines instead of the three in the solution presented.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=988970

Regards!

...JRF...
tom quach_1
Super Advisor

Re: Grep command

Thank you all for your help.
sorry Sandman, you should get 10 points.
my fault, did not run the command correctly.
Your suggestion works good.

Thanks again,
Tom