extract set of matching records

i have a pipe delimited file with records spread in many lines.
i need to extract those records
1)having X in beginning of that record

2)and having at least one Y in beginning before other record begins

eg:
X|Rec1|
A|Rec1|
Y|Rec1|

X|Rec2|
Y|Rec2|

Z|Rec3|

X|Rec4|
M|Rec4|

X|Rec5|

so, lines Rec1,Rec2 have to be in out.DAT and Rec3,Rec4,Rec5 in err.DAT
thanks.

Hi,

try:

awk  'BEGIN{FS="\n";RS=""}\
    /X/ && /Y/{printf "%s\n\n",$0 > "good"};\
    !(/X/ && /Y/){printf "%s\n\n",$0 > "bad"}' file

HTH Chris

thanks Chris. this will surely help. But, if different records are not separated by one or more blank lines, how to isolate records?
like then what will i put in RS?

eg:-
X|Rec1|
Y|Rec1|
X|Rec2|
z|Rec3|

Then you'll have to separate them:

awk -F'[|]|Rec' 'BEGIN{old=1};\
    $3 > old{printf "\n%s\n", $0;old=$3};\
    $3==old{printf "%s\n",$0;old=$3}' file2

and afterwards run the other command.

Tested with:

X|Rec1|
A|Rec1|
Y|Rec1|
X|Rec2|
Y|Rec2|
Z|Rec3|
X|Rec4|
M|Rec4|
X|Rec5|

HTH Chris

nawk 'BEGIN{
RS=""
}
/^X/ && /Y/ {
printf("%s\n\n",$0) >> "out.txt"
next
}
{
printf("%s\n\n",$0) >> "err.txt"
}
' file