in reply to Out of memory
If you know that you are looking for a pattern that will not stretch over more than 2 (or, any n) consecutive records, then you can simply build an array of the first n records in the file (to “prime the pump” ...), then proceed as follows:
No matter how enormous the file being processed may be, the memory requirements are negligible.