Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How do I copy out the last line written in a file?

by Anonymous Monk
on Sep 19, 2000 at 01:41 UTC ( #33034=perlquestion: print w/replies, xml ) Need Help??

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

How do I copy out the last line written in a file?

Originally posted as a Categorized Question.

  • Comment on How do I copy out the last line written in a file?

Replies are listed 'Best First'.
Re: How do I copy out the last line written in a file?
by Fastolfe (Vicar) on Sep 19, 2000 at 01:53 UTC
    There are three commonly proposed methods to do this:

    Read the entire file

    @array = <FILE>; $last_line = $array[$#array];
    This involves reading the entire file before examining the last line, which can be costly in memory and performance for large files.

    Seek to the end and backtrack

    seek(FILE, -81, 2); # 81 bytes from the end, which should +be enough @lines = <FILE>; $last_line = $lines[$#lines]; # similar to above, but we don't read +much
    Use File::ReadBackwards
    tie *FILE, File::ReadBackwards 'input_file'; while (<FILE>) { # line-by-line starting from the end }
    This latter method is supposed to be efficient and fast, so I imagine it uses some variation of the seek() method above.
      When a file is read into an array the last element in the array array-1 would give you what you are looking for. Shashidhar Iddamsetty
        When a file is read into an array the last element in the array array-1 would give you what you are looking for.

        To put it more clearly, and in actual Perl rather than some pseudo-language, the first example above, i.e.

        @array = <FILE>; $last_line = $array[$#array];

        could be rewritten like

        @array = <FILE>; $last_line = $array[-1];

        or even without creating an intermediate @array:

        my $last_line = (<FILE>)[-1];

        Incidentally, I put a my in front of that as a reminder of the good rule of always staying strict.

Re: How do I copy out the last line written in a file?
by merlyn (Sage) on Sep 19, 2000 at 02:07 UTC
    And don't forget File::Tail, which can keep up with the file as it grows (of which this request is often a part).
Re: How do I copy out the last line written in a file?
by I0 (Priest) on Nov 17, 2004 at 00:38 UTC
    use Tie::File:
    use Tie::File; sub last_line_of_file { my $file = shift; tie my @array, 'Tie::File', $file or die "read $file - $!"; $array[-1] }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://33034]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2021-04-21 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?