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

How do I delete the last record of my data in a text file. I have lines of data like this:
3098 4908 3098 54098 34098 2308 9987
I want to delete the last record which will have different numbers but in this case is 9987. This is where i am with script on Windows.
my $stuff = 'file.txt'; open(F, "$stuff") || die "Can not open file: $!\n"; my @records = (<F>); print "@records\n"; s/lastlineofdata//; #This is the part I am struggling with close(F);

Replies are listed 'Best First'.
Re: Delete last record in text file
by jmcnamara (Monsignor) on Aug 18, 2003 at 17:14 UTC

    Here is one way:
    perl -i.bak -ne 'print unless eof' file1 file2 ...
    However, this is inefficient for large files since it reads all the way through the file before ignoring the last line.

    For large files see: Delete the last line of a file.

    --
    John.

Re: Delete last record in text file
by CukiMnstr (Deacon) on Aug 18, 2003 at 17:32 UTC
    you can also use Tie::File:
    #!/usr/bin/perl -w use strict; use Tie::File; my $filename = 'file.txt'; tie my @data, 'Tie::File', $filename or die "couldn't tie(): $!"; pop @data; untie @data;

    hope this helps,

      Tie::File seems to be rather slow on large files. I tried the Tie::File and (open file, read in all lines, pop, write back) variants on a ~5M file, and the second was about 10 times faster.
Re: Delete last record in text file
by dragonchild (Archbishop) on Aug 18, 2003 at 17:16 UTC
    my $stuff = 'file.txt'; open(F, "$stuff") || die "Can not open file: $!\n"; my @records = (<F>); print "@records\n"; s/lastlineofdata//; #This is the part I am struggling with close(F);

    Some notes:

    • Each line is in one element of the array.
    • You want to delete the last line.

    Sounds like you want to delete the last line of the array.

    Now, just because you do that, you aren't actually affecting the file. You would have to write back to the file what the file should now look like.

    use strict; use warnings; use IO::File; my $filename = 'file.txt'; my $in_fh = IO::File->new($filename) || die "Can not open '$filename' for reading: $!\n"; my @records = <$in_fh>; $in_fh->close; print "@records\n"; pop @records; my $out_fh = IO::File->new(">$filename") || die "Cannot open '$filename' for writing: $!\n"; print $out_fh, @records; $out_fh->close;
    You can do this in a 1-line from the command-line.
    perl -pi -e 'last if eof()' file.txt

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Delete last record in text file
by blue_cowdawg (Monsignor) on Aug 18, 2003 at 17:16 UTC

    Pretty easy solution actually. Check out the following:

        #!/usr/bin/perl -w use strict; my @lines=<DATA>; pop @lines; open(FOUT,"> myout.txt") or die $!; print FOUT @lines; close FOUT; __END__ line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 11 line 12 line 13 line 14 line 15

    Here is the output:

        --$ cat myout.txt line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 11 line 12 line 13 line 14

    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Nobody expects the Perl inquisition!

    Test the code? We don't need to test no stinkin' code!
    All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales