| Author |
file handle problem
|
|
| newbie 2004-12-07, 4:11 am |
| Thanks guys, I get the point of not putting large file in memory. I got a
new question. Say I have a bunch of large files, and I want to just get rid
of the last line of each of these large files. Is there a simpler way
without reading in and outputing the file?
Thanks.
| |
| Gunnar Hjalmarsson 2004-12-07, 4:11 am |
| newbie wrote:
> Thanks guys, I get the point of not putting large file in memory. I got a
> new question. Say I have a bunch of large files, and I want to just get rid
> of the last line of each of these large files. Is there a simpler way
> without reading in and outputing the file?
Check out Tie::File.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| John W. Krahn 2004-12-07, 4:11 am |
| newbie wrote:
> Thanks guys, I get the point of not putting large file in memory. I got a
> new question. Say I have a bunch of large files, and I want to just get rid
> of the last line of each of these large files. Is there a simpler way
> without reading in and outputing the file?
perldoc -f truncate
John
--
use Perl;
program
fulfillment
| |
| Anno Siegel 2004-12-07, 4:11 am |
| John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:
> newbie wrote:
>
> perldoc -f truncate
....probably in combination with File::ReadBackwards.
Anno
| |
| John W. Krahn 2004-12-07, 4:11 am |
| Anno Siegel wrote:
> John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:
>
>
>
> ...probably in combination with File::ReadBackwards.
The OP said that he didn't want to read in the file so presumably he already
knows the length of the last line?
John
--
use Perl;
program
fulfillment
| |
| Anno Siegel 2004-12-07, 4:11 am |
| John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
>
> The OP said that he didn't want to read in the file so presumably he already
> knows the length of the last line?
What are you doing? Applying logic to Usenet postings? :)
Anno
| |
| Uri Guttman 2004-12-07, 4:11 am |
| >>>>> "AS" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:
AS> John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:[color=darkred]
AS> ...probably in combination with File::ReadBackwards.
and you can use the tell() method to get the s location after reading
in the last line. someone has asked me for a method to get the internal
file handle so he doesn't have to reopen the file. i will be adding that
soonish. then the OP can just do this <untested>:
use File::ReadBackwards ;
my $rb = File::ReadBackwards->new( 'filename' ) ;
$rb->readline() ;
my $tell = $rb->tell() ;
my $fh->handle() ;
$fh->truncate( $tell ) ;
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| John W. Krahn 2004-12-07, 4:11 am |
| Anno Siegel wrote:
> John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:
>
>
> What are you doing? Applying logic to Usenet postings? :)
As well as correct spelling, grammar and punctuation! ;}
John
--
use Perl;
program
fulfillment
| |
| Andrew Hamm 2004-12-07, 4:11 am |
| newbie wrote:
> Thanks guys, I get the point of not putting large file in memory. I
> got a new question. Say I have a bunch of large files, and I want to
> just get rid of the last line of each of these large files. Is there
> a simpler way without reading in and outputing the file?
Did you ever get a solution? Apart from some useful pointers, I can't see
one. There's a few ways. One is a simple re-write loop which stores the
previous line:
my $buffer;
if($buffer = <IFD> ) {
while(<IFD> ) {
print OFD $buffer;
$buffer = $_;
}
}
the last line will not be printed.
The suggestion to use truncate is also reasonable:
$size = 0;
# $pos gets the (potential) start of the next line
while($pos = ftell IFD, <IFD> ) {
# if there really was a next line, save it
$size = $pos;
}
truncate IFD, $size;
NOTE: I haven't actually tested this last one, but it looks reasonable :-)
If it fails repost and I'll put in a bit more effort
|
|
|
|