grep multiple lines

I was afraid my explanation would be vague. Lemme try to be more specific.

Suppose the following three alarms are in a log file:

Wed Feb 2 07:02:22 <vsm> 91041 Line 37 Severity 4 Vps 8
No Host Found - TN:2135572346

Wed Feb 2 07:02:22 <vsm> 91041 Line 23 Severity 4 Vps 3
No Host Found - TN:2156772216

Wed Feb 2 07:02:22 <vsm> 91041 Line 12 Severity 4 Vps 1
No Host Found - TN:2165523456

Now...let's suppose that the only one I really care about is the one in blue. What I want to do is prompt a user to tell me the "Line" number, then the "Vps" number. So for this example, the user would input Line 23 and Vps 3.

The script would then look at all 91041 alarms, find the one(s) for Line 23 and Vps 3, then report them back to me. As a bonus, it would also report the second line of the 91041 alarm to me, which is important because it has the TN. In other words, after running my script, one would see:

Wed Feb 2 07:02:22 <vsm> 91041 Line 23 Severity 4 Vps 3
No Host Found - TN:2156772216

Does that make more sense???

-cd

For fun, watch how easy this is on Linux with GNU grep and the -A option:

That is why I always used to build these GNU utilities everywhere I worked in my sys-admin days. (seems so long ago...LOL)

:rolleyes:

Quite a bit! (Why not do that in the first post?)

#! /usr/bin/ksh

print -n  "Enter Line - "
read line
print -n "Enter Vps - "
read vps
sed -n '/Line '${line}' .*Vps '${vps}'/{N;p;}'   < inputfile
exit 0

That did the trick. Thanks for all the help, guys.

awk '/91030/ {print $0} {getline;print $0}' logxx
:cool:

This is almost exactly what I need, but I need to get the line before the match as well as some lines after it. How would I get the line before the match? I also do not have the -B and -A options or ggrep.
Thanks.

EDIT: I continued playing around with it and beating my head against the wall. I got this and it seems to work:

sed -n -e /regex/ {=;x;p;g;N;p;D;}' -e h searchfile

Here is a usefull script that might be what you need...
It's a perl script that is a windowing grep that is useful for grabbing X number of lines before and after a match.

http://sysunconfig.net/unixtips/wgrep.txt

:cool:

All you need is this command:

awk 'c==1{print $0; c=0} /Search_String/{print $0; c=1}' File_Name.txt

---------------------------------------------------------------------

Here is how it works in action:

> cat file.txt

Apples1: 111
Product Code = 01

Oranges1: 222
Product Code = 02

Grapes1: 333
Product Code = 03

Apples2: 111
Product Code = 04

Oranges2: 222
Product Code = 05

Grapes2: 333
Product Code = 06

> awk 'c==1{print $0; c=0} /Oranges/{print $0; c=1}' file.txt
Oranges1: 222
Product Code = 02
Oranges2: 222
Product Code = 05