hj4jc has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: truncate only a last blank line
by choedebeck (Beadle) on Dec 28, 2005 at 22:57 UTC | |
by hj4jc (Beadle) on Dec 29, 2005 at 15:35 UTC | |
|
Re: truncate only a last blank line
by swampyankee (Parson) on Dec 28, 2005 at 23:56 UTC | |
|
Re: truncate only a last blank line
by smokemachine (Hermit) on Dec 28, 2005 at 23:23 UTC | |
|
Re: truncate only a last blank line
by Aristotle (Chancellor) on Dec 29, 2005 at 16:31 UTC | |
|
Easy if you don't mind slurping.
by kbrint (Sexton) on Dec 29, 2005 at 22:16 UTC | |
by Aristotle (Chancellor) on Dec 30, 2005 at 05:29 UTC | |
|
Re: truncate only a last blank line
by tcf03 (Deacon) on Dec 29, 2005 at 19:04 UTC | |
by tcf03 (Deacon) on Dec 30, 2005 at 03:18 UTC | |
|
Re: truncate only a last blank line
by tcf03 (Deacon) on Dec 30, 2005 at 03:14 UTC |