in reply to truncate only a last blank line
No need to seek through the file. You already know that what you want to get rid of, if it’s there at all, is at the very end.
#! usr/bin/perl use strict; use warnings; # length of end-of-line marker my $eol_len = length $/; opendir my $dh, "." or die "Can't open current directory: $!\n"; while( my ( $file ) = readdir $dh ) { next if $file !~ /\.txt/; open my $fh, '+<', $file or warn( "Can't open $file for read/write: $!\n" ), next; # read data from last possible location of end-of-line seek $fh, -$eol_len, 2; my $tail = <$fh>; # if chomp finds an EOL, rewind and truncate if( chomp $tail ) { seek $fh, -$eol_len, 2; truncate $fh, tell $fh; } }
Makeshifts last the longest.
|
|---|