If the file is so big that you don't have
enough disk space available for both the "before"
and "after" versions, then you can
go to quite a bit of extra work to save disk
space:
my $fileName= "toBeTrimmed.txt";
open( FILE, "+< $fileName" )
or die "Can't update $fileName: $!\n";
my $readPos= tell(FILE);
my $writePos;
while( <FILE> ) {
if( wantLine($_) ) {
$readPos= tell(FILE)
or die "Can't tell after read: $!\n";
if( defined($writePos) ) {
seek( FILE, $writePos, 0 )
or die "Can't seek to write: $!\n";
print FILE $_
or die "Failed writing to file: $!\n";
$writePos= tell(FILE)
or die "Can't tell after write: $!\n";
seek( FILE, $readPos, 0 )
or die "Can't seek to read: $!\n";
}
} elsif( ! defined($writePos) ) {
$writePos= $readPos;
}
}
if( defined($writePos) ) {
truncate( FILE, $writePos )
or warn "Can't truncate: $!\n";
}
But be sure to test this code on a subset of your
file and make a backup of the file beforehand
as a failure of this code will simply leave your
file corrupted.
-
tye
(but my friends call me "Tye")
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|