Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

output preceding line after having grep'd on string

Status
Not open for further replies.

dominicdiddy

Technical User
Jan 5, 2006
14
GB
Hi All,

Any ideas on how to output several lines below or above (or both) when searching for a string in a ASCII file please?

Example below,

---

Thu Nov 24 07:45:36 2005 hdgej
hdgej: New journal file opened.
hdgej: /lbtest/db/file.test.out lgdmn: version 1, sequence 5111, volume 1.

----

So if having searched for string, say "version 1", then how could I output the entire record (ie the 4 lines) surrounded by blank lines...

Any help greatly appreciated!!!

Thanks in advance,

Dominic
 
You may try this:
awk 'BEGIN{RS=""}/version 1/' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Maybe some options ?
man grep said:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing -- between con­tiguous groups of matches.

-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing -- between contiguous groups of matches.

-C NUM, --context=NUM
Print NUM lines of output context. Places a line containing -- between contiguous groups of matches.

Feherke.
 
Afternoon all,

Once I have found the entries with the "emergency" string in them, how would I just select the ones for today and then echo a staement if found?

Any help greatly appreciated...
 
re_today=$(date +'%a %b %d ..:..:.. %Y')
grep -p "version 1" file|egrep "^${re_today}"

should do it.


HTH,

p5wizard
 
Thanks for that but now getting this... Any ideas?

echo $DATE
+ echo Jan 13
Jan 13
# grep -p "emergency" /lb/db/file.ral | egrep "^${DATE}"
+ grep -p emergency /lb/db/file.ral
+ egrep ^Jan 13
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
/lbprod/db/file.ral: 0652-034 The maximum paragraph length is 10,000 bytes.
#
# grep -p "emergency" /lb/db/file.ral | egrep "^${DATE}
 
Some of your paragraphs are too long...

either ignore, if your paragraphs of interest are small enough, or use PHV's awk solution to get the paragraphs of interest:

awk 'BEGIN{RS=""}/version 1/' /path/to/input | grep "^${DATE}"

or

grep -p "version 1" /path/to/input 2>/dev/null | grep "^${DATE}"


but I would also put the weekday in your date search string if you're grepping for the date at the beginning of the line (^):

DATE="Fri Jan 13"

substitute "version 1" for "emergency" if that's what you're really after...


HTH,

p5wizard
 
sorry but....

# awk 'BEGIN{RS=""}/emergency/' /lb/db/file.ral | grep "^${DATE}"
+ awk BEGIN{RS=""}/emergency/ /lb/db/file.ral
+ grep ^Fri Jan 13
awk: 0602-534 Input line Mon Sep 19 19:09:49 cannot be longer than 10,239 bytes
.
The input line number is 580. The file is /lb/db/file.ral.
The source line number is 1.

why on Fri afternoon???!
 
Well it was friday 13th, right?

You might want to reduce line length for awk to process by first fold-ing the input file into smaller lines...

fold -w 1000 /path/to/inputfile | awk 'BEGIN...


HTH,

p5wizard
 
Here's the method I prefer to use ...
Code:
tail +$((`grep -n 'emergency' /lb/db/file.ral | cut -d: -f1` - 5)) /lb/db/file.ral | head

It may be a little inefficient, but I think it does what you want. It will give ten lines of the file, starting five lines before your matched expression.

[ol]
[li][tt]tail +#[/tt] gives the end of a file starting at # lines from the beginning of the file.[/li]
[li][tt]grep -n[/tt] precedes it's output with the line number of the match and a colon.[/li]
[li][tt]cut -ddelimiter -f#[/tt] uses the colon as a delimiter and returns the first field.[/li]
[li][tt]$((expr))[/tt] is a bashism that does some math for you. I think it works in Korn, too. If that one doesn't work for you, I'm sure there are other ways to get the math done. So, my sample subtracts 5 from the line number.[/li]
[li]The second occurrence of the filename is an argument to the tail command at the beginning of the line.[/li]
[li]Pipe (|) the whole mess through head, which defaults to 10 lines.[/li]
[/ol]
So, you get 10 lines, starting at 5 lines before the match. If you want more, or different, lines, be sure to adjust both the 5 in the double-parentheses and add a -n to [tt]head[/tt].

My grep command doesn't have a [tt]-p[/tt] argument... what's it do?

--
-- Ghodmode
 
on AIX - grep -p does a paragraph search - paragraphs containing a RE

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top