in reply to truncate only a last blank line

Keeping your code mostly the same you could do something like this.
#! usr/bin/perl opendir(FILES,".")||die "Cannot open files in the directory\n"; @files=(); for(readdir(FILES)){ if($_=~/\.txt/){ push(@files, $_); } } print "@files\n"; for $i(0..$#files){ print "working on $files[$i]\n"; open (FH, "+<$files[$i]") or die "can't update $file: $!"; my $linevalue; while ( $linevalue = <FH> ) { $addr = tell(FH) unless eof(FH); last if (eof(FH)); } if($linevalue eq "\n") { truncate(FH, $addr) or die "can't truncate $file: $!"; } exit;

There are probably better ways to do this, but I don't see why this one wouldn't work.

Replies are listed 'Best First'.
Re^2: truncate only a last blank line
by hj4jc (Beadle) on Dec 29, 2005 at 15:35 UTC
    Thank you so much for your reply.
    I really don't know how to thank you enough!
    This script did exactly what I needed to do.
    I modified it a little bit at the end.
    Thank you again!

    #! usr/bin/perl opendir(FILES,".")||die "Cannot open files in the directory\n"; @files=(); for(readdir(FILES)){ if($_=~/\.txt/){ push(@files, $_); } } print "@files\n"; for $i(0..$#files){ print "working on $files[$i]\n"; open (FH, "+<$files[$i]") or die "can't update $file: $!"; my $linevalue; while ( $linevalue = <FH> ) { $addr = tell(FH) unless eof(FH); last if (eof(FH)); } if($linevalue =~ /^\s*$/) { truncate(FH, $addr) or die "can't truncate $file: $!"; print "truncated the last blank line in $files[$i]\n"; }else{ print "$files[$i] does not end with blank line\n"; } } exit;