On Tue, Jul 31, 2001 at 04:46:59PM -0400, Adam Witney wrote:
>
> Hi,
>
> I have a file which contains about 45000 lines of 50 characters each. I want
> to reformat it to contain 60 characters each.
>
> I can read the whole file in and remove the line endings and then print out
> lines of 60 characters, but this can take some time. And I may need to do
> this on bigger files still.
>
> Is there a quicker way?
If memory is the issue, you could read in chunks:
#!perl
open(IN, 'file') or die "Can't open 'file': $!\n";
my $len = 60;
while (read(IN, $buf, 65536, length $buf)) {
$buf =~ tr/\n//d;
print "$1\n" while $buf =~ /\G(.{$len})/gco;
$buf = substr $buf, pos $buf;
}
print "$buf\n" if $buf;
}
__END__
There's probably a faster way, though.
Ronald