open(my $fh_in, '<', ...) or die("Unable to open input file: $!\n"); open(my $fh_good, '>', ...) or die("Unable to open 'good' output file: $!\n"); open(my $fh_bad, '>', ...) or die("Unable to open 'bad' output file: $!\n"); binmode($fh_in); binmode($fh_good); binmode($fh_bad); my $buf = ''; my $skip = 0; local $/ = "\r\n"; # Because we're using binmode. my $rec_len = 1000 + length($/); # Buffer size can be up to $blk_size + $rec_len bytes. my $blk_size = 8192; for (;;) { my $read = read($fh_in, $buf, $blk_size, length($ofs)); defined $read or die("Unable to read input file: $!\n") while (length($buf) >= $rec_len) { my $pos = index($buf, $/); if ($pos < 0) { print $fh_bad $buf; $buf = ''; $skip = 1; } else { $pos += $ofs + length($/); my $bad = $skip || $pos != $rec_len; print { $bad ? $fh_bad : $fh_good } (substr($buf, 0, $pos)); substr($buf, 0, $pos, ''); $skip = 0; } } if ($read == 0) { print $fh_bad $buf if length $buf; last; } }

Untested.

Update: Cleanup.


In reply to Re: Thrashing on very large lines by ikegami
in thread 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.