Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to be able to chop the beginning of a file off without actually reading it. I could read the file, discarding the first n lines, and write the remainder back out to the same file (or a new file and rename it). But that seems really lame to me - I want better performance! Essentially, I want to change the directory entry to point to a new starting point for the file, and then release the area no longer in use to the pool of unallocated disk space. I only need this on linux, but I'd prefer portable code since I am OS-agnostic. Any help would be greatly appreciated... thanks for your time!

Replies are listed 'Best First'.
Re: pretrunkulation?
by YuckFoo (Abbot) on Feb 07, 2002 at 21:04 UTC
    If you have fixed length records, you can seek to the position you want, but you will still have to read/rewrite the remainder of the file.

    Basically TINWTDI (there is no way...) using the method you have asked for.

    For discussion of what you can do: perldoc -q 'delete a line'.

    YuckFoo

Re: pretrunkulation?
by talexb (Chancellor) on Feb 07, 2002 at 20:22 UTC
    You probably want to do something like this:
    sub ProcessFile { my ( $FileName ) = @_; # Open file for reading and writing, slurp entire file into an arra +y. open ( HTML, "+<$FileName" ) or die "Cannot open $FileName for read/write: $!"; my @Line = <HTML>; # Seek to the beginning of the file and truncate the file, ready to # write the updated information back in. seek ( HTML, 0, 0 ); truncate ( HTML, 0 ); # Go through the file, deleting zero height and width restriction o +n IMG # tag, then write corrected line out to the file. my $Count = 0; foreach ( @Line ) { $Count += s/\r//; print HTML $_ or die "Unable to output to $FileName: $!"; } print " $Count"; # Close the file. close ( HTML ); }
    I can't advise you strongly enough to forget about modifying the OS' internal file system to achieve the same objevtive. Surely that way madness lies.

    And don't assume you'll never need this under anything than Linux. Perl makes it simple to write portable code -- why mess with that?

    ps Apologies for the mis-match between comments and code -- I had to write a bunch of these filters quickly.
    pps Yes, I could have used tr/// instead of s/// there. I said I was in a rush.

    --t. alex

    "Of course, you realize that this means war." -- Bugs Bunny.

Re: pretrunkulation?
by Anonymous Monk on Mar 21, 2002 at 18:44 UTC
    Well, I knew those techniques, but they are what I'm trying to avoid. I'm looking for a more elegant solution, but I guess Perl can't do it. Oh well, I'll have to write it in C then, and it'll probably have to be completely hardware and OS specific (boohoo). Thanks anyways guys.