grep multiple lines

Hey guys:

I've been meaning to post this question for awhile...it is regarding grep. Let's say for example that the following entry is in logxx:

Wed Feb 2 07:44:11 <vsm> 91030 Line 5 Severity 1 Vps 6
Call Answered - DN:8753101 CLID:5164665761 PI:83

If I do a grep 91030 logxx, I will get the following:

Wed Feb 2 07:44:11 <vsm> 91030 Line 5 Severity 1 Vps 6

However, what I really want are BOTH lines. Is there a way to grep and have it return both lines??

Thanks,

-cd

Pls dont mind if this code seems stupid:

grep -i 91030 logxx >> file
grep -v 91030 logxx >> file

Thanks for the attempt, but that did not do the trick.

-cd

Please try:

grep -A1 91030 logxx

generic:

grep -A1 PATTERN [FILE...]

That did not work either. Here's the error:

grep: illegal option -- A
grep: illegal option -- 1
Usage: grep -hblcnsviw pattern file . . .

Here's my box info:

ROOT@arivrc: uname -a
SunOS arivrc 5.6 Generic_105181-35 sun4u sparc SUNW,UltraSPARC-IIi-Engine

Assuming that the keyword in the second line is also fixed as is the one in the first line

/usr/xpg4/bin/grep -e <keyword1> -e <keyword2> <filename>

For your file, Let's say
/usr/xpg4/bin/grep -e 91030 -e Answered logxx

That did not work either. Let me see if I can be a little more specific.

Let's say I want to grep xx from logyy. I want not only the instance of xx to return, but the line directly below it as well (no matter what it is).

So basically, I am telling the shell..."find xx in my log yy. Every time you find it, show it to me, and while your at it, give me the line below it as well".

Does that make more sense??

-cd

If it works for your version of grep the -A and -B flags specify lines before and after a pattern match.
You can also do it with sed

sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

For other tricks with sed look here:

http://www.student.northpark.edu/pemente/sed/sed1line.txt

Yes,

grep with -A is a Linux/GNU version of grep. If you know my posts, I am a big GNU fan. Part of their man page:

Edited Note: Sorry, I checked and the Solaris GNU version may not support the -A option either. - Neo

BTW, I recall that GNU grep in Solaris is called:

ggrep

Do you have it?

Note: Sorry, I checked and the Solaris GNU version may not support the -A option either. - Neo

Please note the OS X version of the MacOX supports the -A grep option:

MacOS X Man Page on Grep

I kinda like that. But I think all he wants is just...
sed -n '/regexp/{N;p;}'

From GOOGLE:

How do I grep a file for "pattern" so the output shows the 3 lines above and below each "pattern" line (including "pattern" line)? I used to have a simple grep/sed/awk to do it but I lost it. I thought it used grep and tail, but so far my attempts don't work.

Response Number 1

does your "man grep" discuss "context"?
if not:
ex -R file<<!
g/pattern/.-3,.+3p
q
!

Response Number 2

Colourised awk version:
awk 'BEGIN {c=3};/PATTERN/ {p=c+1;$0="\033[31m"$0"\033[m"};{if (p==0 || p==c+1) {buf=buf"\n"$0;b++};if (b>c+1) {b--;sub(/\n[^\n]*\n/,"\n",buf)};if (p==c+1) {sub(/^\n/,"",buf);print buf;b=0;buf="";p-- ;next};if (p>0) {print;p--}}' file

Replace 'c=3' for different amounts of context, replace 'PATTERN' with your pattern and replace '31m' with the colour you prefer or remove everything except 'p=c+1' in that action bracket if you don't want colours.

Response Number 3

grep -A3 -B3 <pattern> filename
A-After
B-Before

The sed idea is definitely a step in the right direction. I may very well be able to use that. However, it may get a little tricky with the regexp portion as seen below in red:

sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

What if I have prompted users for input prior to this portion of the script, and I want to read in (for example) $NAME and $ADDRESS in addition to the regexp I told you guys about earlier?? I tried enclosing everything in double quotes, single quotes, and backticks.

M

I do not have ggrep

regexp=something
sed -n /$regexp'/{N;p;}'

perderabo seems to get it all..This should work..

Close. VERY close. I am almost there.

Now, Perderabo, let's say I have several "somethings"...kinda like this:

regexp=something
regexp2=anothersomething
regexp3=lastsomething

How would I use the sed -n /$regexp'/{N;p;}' in that case????

-cd

:confused: I have no idea what that's supposed to mean...but maybe...

for regexp in something anothersomething lastsomething ; do
        sed -n /$regexp'/{N;p;}'  <  datafile
done

Try this:

#!/bin/ksh
while read regexp;do
sed -n /$regexp'/{N;p;}
done

name this file regexp.ksh and execute this as

. regexp.ksh < regexp.txt

where regexp.txt has all those
something
anothersomething
lastsomething