Grep for a pattern and print entire record

Hi friends,
This is my very first post on forum, so kindly excuse if my doubts are found too silly.
I am trying to automate a piece of routine work and this is where I am stuck at the moment-I need to grep a particular ID through a file containing many records(which start with <LRECORD> and end with </LRECORD>), and print only those records which have the matching ID.
As an example, my input file contents are as below:

<LRECORD>alskdkfdsfk
fsdjklfnsdf
fsjdfndjn</LRECORD>
fdsfdfg
<LRECORD>123
f42
5345</LRECORD>

Now supposing I need to print only the record containing f42, how do I do that? :confused:

Dear Faiz,,

If the lines between the &lt;LRECORD&gt; &lt;/LRECORD&gt; pair is unique you can use the -A and -B option of the grep command. For example in your post the lines b/w the&lt;LRECORD&gt; &lt;/LRECORD&gt; pair is 1 so you can use the command:

                         grep 'f42' ./input.txt -A 2 -B 1

Hope this will help.

If you want to find the record containing f42, you can do like this:

awk 'BEGIN{RS="(<LRECORD>|<\/LRECORD>)"; FS="";}/f42/ {print $RS} ' yourfile.txt

@thanhdat
This is not working for me.

I suggest this...

awk '/<LRECORD>/ {
   RS="</LRECORD>"
   FS="LRECORD>"
}
/f42/{ print $2
}' input

My awk version is: mawk 1.3.3 (default on Debian Lenny). I also tested with gawk 3.1.5 and both your solution and mine work ^^
What's your awk version and what was the error message ? Maybe I can avoid it next time. Thankssss

I dont know my version of awk...
The error was...

awk: Field (<LRECORD>|</LRECORD>) is not correct.
 The input line number is 1. The file is datafile.txt.
 The source line number is 1.

@rakeshawasthi: maybe some awk versions don't support the syntax "\/LRECORD" �__� I'll try to use another way next time. Thanks for your report.