I am trying to remove the last line of many files, but only if that last line is a blank line. What I have here will truncate any last line, which I would like to revise.
I can see that $addr is a position pointer, while what I'm interested in is the content of the last line...#! 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: $!"; while ( <FH> ) { $addr = tell(FH) unless eof(FH); } truncate(FH, $addr) or die "can't truncate $file: $!"; } exit;
The good thing about the above code is that it's very fast. Each of the file I have contains more than 30K lines, and I have hundreds of them, so I really don't want to do this by hand.
I would appreciate any help with this...
Thank you,
Hyunjin Choi
In reply to truncate only a last blank line by hj4jc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |