0

I have several commands printing text to a file using perl. During these print commands I have an if statement which should delete the last 5 lines of the file I am currently writing to if the statement is true. The number of lines to delete will always be 5.

if ($exists == 0) {
  print(OUTPUT ???) # this should remove the last 5 lines
}
2

6 Answers 6

4

You can use Tie::File:

use Tie::File;
tie my @array, 'Tie::File', filename or die $!;

if ($exists == 0) {
    $#array -= 5;
}

You can use the same array when printing, but use push instead:

push @array, "line of text";
1
  • As soon as I read the title I thought "Tie::File FTW!" You beat me to it. +1 Feb 17, 2012 at 14:32
3
$ tac file | perl -ne 'print unless 1 .. 5' | tac > file.tailchopped
6
  • 1
    @TLP: head: unknown option -- - and usage: head [-count | -n count] [file ...]. That’s not POSIX compatible.
    – tchrist
    Feb 17, 2012 at 20:57
  • 1
    @TLP So what? That’s not POSIX. It’s not standard. It’s a prosaic local variant. I have at least 3 systems where it fails miserably. You should not expect other people to run non-standard software. People who live in the Linux glasshouse don’t know what the real world outside is like.
    – tchrist
    Feb 18, 2012 at 1:36
  • 2
    I'd take head -n -5 over tac in a portability contest any day. Feb 18, 2012 at 3:06
  • 2
    @socketpocket Just alias tac to perl -e 'print reverse <>' — I always do. :) Honestly, I probably had that for decades before tac (1) started shipping. Heck, I had a tac before Perl, for goodness’ sake!
    – tchrist
    Feb 18, 2012 at 3:50
  • 1
    @TLP: if you are going to suggest standard Unix tools, provide a version that doesn't use long options (as socket puppet has done). Long options are a GNU extension, and not portable.
    – ninjalj
    Feb 19, 2012 at 15:53
1

Only obvious ways I can think of:

  1. Lock file, scan backwards to find a position and use truncate.
  2. Don't print to the file directly, go through a buffer that's at least 5 lines long, and trim the buffer.
  3. Print a marker that means "ignore the last five lines". Process all your files before reading them with a buffer as in #2

All are pretty fiddly, but that's the nature of flat files I'm afraid.

HTH

3
  • Not that these are "bad" suggestions, but Tie::File has the magic built-in. Feb 17, 2012 at 14:34
  • @Joel Berger, Not it doesn't. Tie::File reads the file from the start, so it would definitely be much slower than (1) for large files. Not to mention all the memory it would use up from creating an index of every line in the file.
    – ikegami
    Feb 17, 2012 at 21:46
  • @ikegami, you are right about #1 being faster (for large files). All I had actually meant was that Tie::File would do most of the work, not that it would be more efficient Feb 17, 2012 at 22:54
1

As an alternative, print the whole file except last 5 lines:

open($fh, "<", $filename) or die "can't open $filename for reading: $!";
open($fh_new, ">", "$filename.new") or die "can't open $filename.new: $!";
my $index = 0; # So we can loop over the buffer
my @buffer;
my $counter = 0;
while (<$fh>) {
    if ($counter++ >= 5) {
        print $fh_new $buffer[$index];
    }
    $buffer[$index++] = $_;
    $index = 0 if 5 == $index;
}
close $fh;
close $fh_new;
use File::Copy;
move("$filename.new", $filename) or die "Can not copy $filename.new to $filename: $!";
4
  • This is #2 from Richard Huxton's answer
    – DVK
    Feb 17, 2012 at 14:53
  • $index = 0 if 5 == $index can also be written $index %= 5, assuming increments of one.
    – TLP
    Feb 17, 2012 at 15:22
  • 1
    Those are very strange open calls. open returns an undefined true value on success and zero on failure - neither of which are useful as file handles. $file is where the filehandle should be, but it is undeclared and you use the same variable twice. Also $filename.new requires double-quotes around it or it won't compile.
    – Borodin
    Feb 17, 2012 at 15:25
  • @Borodin - brain hiccups - didn't have caffeine yet. You're absolutely right.
    – DVK
    Feb 17, 2012 at 16:21
1

File::ReadBackwards+truncate is the fastest for large files, and probably as fast as anything else for short files.

use File::ReadBackwards qw( );

my $bfh = File::ReadBackwards->new($qfn)
   or die("Can't read \"$qfn\": $!\n");

$bfh->readline() or last for 1..5;

my $fh = $bfh->get_handle();
truncate($qfn, tell($fh))
   or die $!;

Tie::File is the slowest, and uses a large amount of memory. Avoid that solution.

0
0

you can try something like this:

open FILE, "<", 'filename';
if ($exists == 0){
 @lines = <FILE>;
 $newLastLine = $#lines - 5;   
 @print = @lines[0 .. $newLastLine];
 print "@print";
}

or even shortened:

open FILE, "<", 'filename';
@lines = <FILE>;
if ($exists == 0){
 print "@lines[0 .. $#lines-5]";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.