in reply to Thrashing on very large lines
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Thrashing on very large lines
by chr1so (Acolyte) on Apr 20, 2006 at 20:12 UTC | |
by ikegami (Patriarch) on Apr 20, 2006 at 21:48 UTC | |
by chr1so (Acolyte) on Apr 21, 2006 at 00:47 UTC |