I have a simple problem and a solution that is too simple..

I have a simple fixed length file of about 1GB in size, with records 1001 bytes long. However, a few lines are the wrong size. I want to separate them into two files, good.txt and bad.txt.

I started with the following, which works on a small test file perfectly:

perl -ne 'print and next if length($_)==1001; print STDERR $_' suspect.txt > goodrecs.txt 2> badrecs.txt

The problem seems to be that there is a bad record in the real-world file that is around 250MB long and near the end. So it looks like Perl is thrashing for hours trying to read that line into $_. (In Windows XP, the memory usage bounces up and down 100MB every few seconds, and it's reading about 8k per second.)

I tried adding BEGIN {$|++} so at least it'd flush the buffer to the goodrecs.txt before thrashing, but it still stops writing mid-record. (But it does write a little more.)

What can I do? And what is a general solution? :)

Any guidance is appreciated, oh Perl monks.


In reply to Thrashing on very large lines by chr1so

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.