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")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"; }
In reply to Re: What is the best way to remove(delete) specific lines from a very large file?
by tye
in thread What is the best way to remove(delete) specific lines from a very large file?
by Flame
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |