This came up as a result of Deleting last line of file.

The simple way to delete the last line of a file is:     perl -i -ne 'print unless eof' file

The following program is an alternative approach which is faster for large files. It reads from the end of the file in buffer sized chunks until it encounters the last line and then truncates the file to delete it. It also works with Windows files.

    Usage: perl truncate.pl file See File::ReadBackwards for a more rigorous treatment of this type of problem.

#!/usr/bin/perl -w # Simple program to delete the last line of a file. # Reads from the end of the file for effeciency # # reverse('©'), MMII, John McNamara, jmcnamara@cpan.org use strict; my $filename = shift or die "Usage: $0 file\n"; my $file_size; my $buff_size = 256; my $eol = "\n"; my $eol_size = length $eol; my $read_num = 1; my $text = ''; # Open the file in read/write mode open FILE, "+<$filename" or die "Couldn't open $filename: $!"; $file_size = -s $filename; binmode FILE; # Change the buffer size to equal the file size if the file # is smaller than the buffer. $buff_size = $file_size if $buff_size > $file_size; # Read backwards through the file until we find an eol while (($buff_size <= $file_size) and ($file_size > 0)) { # Rewind from the end of the file seek FILE, -$buff_size *$read_num, 2; # Store the current position my $current = tell FILE; # Ignore possible eol as last char on first read if ($read_num == 1) { read FILE, $text, $buff_size -$eol_size; } else { read FILE, $text, $buff_size; } # Find the last eol and truncate the file after it if ((my $offset = rindex $text, $eol) != -1) { truncate FILE, $current +$offset +$eol_size; last; } elsif ($current <= $buff_size) { # If the current file position is within the buffer # size and we haven't found an eol then there is only # one line in the file. So we truncate the whole file. truncate FILE, 0; last; } # If eol not found read backwards another time $read_num++; $file_size -= $buff_size; }

In reply to Delete the last line of a file by jmcnamara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.