Perl: Printing Multiple Lines after pattern match

[SIZE=2][SIZE=2]Hello People,
Need some assistance/guidance.
OUTLINE:
Two files (File1 and File2)
File1 has some ids such as
009463_3922_1827
897654_8764_5432
File2 has things along the lines of:
Query= 009463_3922_1827 length=252
(252 letters)

More stufff here

Query= 009525_3967_2963 length=249 uaccno=FIFOXZ216JYL81
(249 letters)
AND MORE STUFF HERE
-----------
PROBLEM:
Capture/finding the Ids stored in File1 from file2 is trivial.
What I need to capture "also" is the remaining part.
For example:
This part of the code gives me the line when it has found the match: Query= 009463_3922_1827 length=252 uaccno=FIFOXZ216JUM5H
while ($line2=<INFILE2>)
{

             if \($line2 =~ /$line1/\) 
             \{ 
                     print $line2; 
             \} 

Now how can I get to the other lines below this (Query= 009463_3922_1827 length=252 uaccno=FIFOXZ216JUM5H) line.
For example, everything until
Query= 009525_3967_2963 length=249 uaccno=FIFOXZ216JYL81
(249 letters)
>>>>>>>>>>>>>>>
a) Few ideas I can think of is using SEEK/tell.
Will this be a efficient way, how much to SEEK, the while loop is reading one line at a time so, some how buffer everything until see the pattern as Query=.....
How to find the bytes until then?

b) Using read()
How to find the number of byes after the pattern match?

c)Using the metacharacters to read ahead after the pattern match /ID (?=SOMETHING)/
Tried this but with until, but its not working. May be my regex is incorrect.

If any one can just be a push in the write direction--pseudocode etc. it would be much appreciated.

I am not reading the files or going to use array (copying the contents of a file to an array) as the files are big.

Since you've solved the first part of the problem, given below is one way to solve the second part.

Assuming that you *know* your ID (from file1), the perl one-liner below fetches the multiline pattern associated with it:

$ 
$ cat file2
Query= 009463_3922_1827 length=252
(252 letters)

Sequences producing significant alignments: (bits) Value

ref|NZ_ACCL02000008.1| Bryantella formatexigens DSM 14469 B_form... 153 2e-37

Query: 243 cccgcacacg 252
|||||||||
Sbjct: 89219 accgcacacg 89228
More stufff here

Query= 009525_3967_2963 length=249 uaccno=FIFOXZ216JYL81
(249 letters)
AND MORE STUFF HERE 
$ 
$ # try the first ID
$ perl -ne 'BEGIN{undef $/; $x="009463_3922_1827"}{while (/(Query= $x.*)Query=|(Query= $x.*)/msg){ print $1 eq "" ? $2 : $1 }}' file2
Query= 009463_3922_1827 length=252
(252 letters)

Sequences producing significant alignments: (bits) Value

ref|NZ_ACCL02000008.1| Bryantella formatexigens DSM 14469 B_form... 153 2e-37

Query: 243 cccgcacacg 252
|||||||||
Sbjct: 89219 accgcacacg 89228
More stufff here

$ 
$ # another ID
$ perl -ne 'BEGIN{undef $/; $x="009525_3967_2963"}{while (/(Query= $x.*)Query=|(Query= $x.*)/msg){ print $1 eq "" ? $2 : $1 }}' file2
Query= 009525_3967_2963 length=249 uaccno=FIFOXZ216JYL81
(249 letters)
AND MORE STUFF HERE 
$ 
$ 

tyler_durden

Hi Tyler,
Hi did not quite get the regex you have said. Why is there a "bit" operator (|) in the regular expression ((Query= $x.)Query=|(Query= $x.)
The idea was IDs in File1 and lots of Stuff in File2 ^^
Based on the IDs in File1 find the IDs in File2 and then print the whole stuff until other IDs start.

To handle the cases when the record's location is intermediate as well as the last.

Right, but this "lots of stuff" is not structured as it does not have a true record separator. Things would've been simpler otherwise.

tyler_durden

Thanks, I really appreciate it.
There is one other thing. You have undef $\. Which is good if the file is small. But the files are going to grow and this is why I am trying to go line by line using the while loops. The only trouble is catching the next "n" lines before the start of other Query=

I have it working now...