grep multiple lines

Hi.

I have this format on a textfile:

VG Name                     /dev/vg00
   PV Name                     /dev/dsk/c16t0d0
   PV Name                     /dev/dsk/c18t0d0
   PV Name                     /dev/dsk/c16t4d0
VG Name                     /dev/vg01
   PV Name                     /dev/dsk/c18t2d0
   PV Name                     /dev/dsk/c16t2d0
VG Name                     /dev/vgsapQ45
   PV Name                     /dev/dsk/vpath8
   PV Name                     /dev/dsk/vpath6
   PV Name                     /dev/dsk/vpath7
   PV Name                     /dev/dsk/vpath5
VG Name                     /dev/vgsapT45
   PV Name                     /dev/dsk/vpath18
   PV Name                     /dev/dsk/vpath16
   PV Name                     /dev/dsk/vpath17

I want to extract the "VG Name" BUT only if the line under have "vpath" in it.

So the output I want is for this:
VG Name /dev/vgsapQ45
VG Name /dev/vgsapT45

I tried using regular expression but cant get it to span across multiple lines.
Anyone have an easy solution for this? awk, grep or egrep ???

/Jocke

You can do something like that (not tested) :

awk '
     /VG Name/ {vg_name = $3 }
     /vpath/ && vg_name { print $0 ; vg_name = "" }
    ' input_file

Jean-Pierre.

sed -n '/^VG/{N;/vpath/s/\n.*//p}'

I dont get any one to work?

Im using Solaris

Try to use nawk instead of awk (I have modified my awk program, 'VG NAme' replaced by 'VG Name')
The sed solution works fine on my system.

Jean-Pierre.

Reborg,

Can you please explain your code?

Thanks

sure thing

sed -n '/^VG/{N;/vpath/s/\n.*//p}'

/^VG/ - search for lines starting with VG
{} - apply the enclosed when the preceeding match is made
N - append the next line to the pattern buffer
/vpath/ - search for vpath within the pattern buffer
s/\n.*//p - delete everything following the first \n then print the buffer.

depending on the system the code may aso need to be written as below becasue \n may not always be recognised.

sed -n '/^VG/{N;/vpath/s/
.*//p}'