Need command for grepping pattern lines with subsequent lines

Hi,

I have a requirement like, I have a list of pattens in a file say pattern.txt,

PHC111
PHC113

and in another file called master.lst i have entries like,

PHC111
a
b
PHC112
a
PHC113
b
c
PHC114
d
e
PHC115

now i want to use the pattern.txt file as patterns and grep the lines in master.lst so that i will get output like,

PHC111
a
b
PHC113
b
c

Please suggest the suitable command. Thanks.

What have you tried?

Do the PHC lines ALWAYS start with "PHC"?
It helps to exactly describe the problem.

Also, what's your OS? (more specifically, do you have GNU grep?)

you can use this code if your PHC line always starts with "PHC"

here pattern.txt contain your pattern list
and mint.txt contains your data

while read pat
do
count=2
while read line
do
b=`echo $line|grep PHC`
if [ $? -eq 0 ]
then
if [ $count -eq 1 ]
then
break;
break;
fi
a=`echo $line|grep $pat`
if [ $? -eq 0 ]
then
count=`echo "$count-1"|bc`
fi
fi
if [ $count -eq 1 ]
then
echo $line
fi
done<mint.txt
done<pattern.txt

Using while loop:

while read line;do egrep -A2 $line file2;done < file1

PHC111
a
b
PHC113
b
c

Using awk:

awk 'FNR==NR {a[$1];next} $1 in a {print;getline;print;getline;print}' file1 file2

PHC111
a
b
PHC113
b
c