Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Read Last Line of A File Only

by Anonymous Monk
on Aug 23, 2004 at 17:36 UTC ( [id://385159]=perlquestion: print w/replies, xml ) Need Help??

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

I want to read the very last line of the file only.
How do I accomplish this in Perl??

Replies are listed 'Best First'.
Re: Read Last Line of A File Only
by Aristotle (Chancellor) on Aug 23, 2004 at 17:53 UTC
    open my $fh, "<", $filename or die "Can't open $filename: $!\n"; my $lastline; $lastline = $_ while <FILE>; print "$lastline\n";

    Or

    use Tie::File; tie my @file, 'Tie::File', $filename or die "Can't open $filename: $!\n"; print @file[ -1 ];

    Or

    use File::ReadBackwards; tie *FH, 'File::ReadBackwards', $filename or die "Can't open $filename: $!\n"; my $lastline = <FH>; print $lastline;

    Or any of a myriad others.

    Which one to pick depends on your precise circumstances.

    Makeshifts last the longest.

Re: Read Last Line of A File Only
by jbware (Chaplain) on Aug 23, 2004 at 17:51 UTC
    I'm sure there are more posts, but doing a quick search on perlmonks, I found "Reading the last line of a file in a Pure Way.". This has some examples as well as links to other posts on the same topic.

    - jbWare

    janitored by ybiC: Changed [http://...] link to Monastery-style [id://...] link, to avoid logging out monks with cookie set to different perlmonks domain

Re: Read Last Line of A File Only
by insensate (Hermit) on Aug 23, 2004 at 18:10 UTC
    perl -ne'print if eof' < my-reasonably-short-file
    Has to read through the entire file... so I'm sure there are more efficient and practical solutions using modules etc...
Re: Read Last Line of A File Only
by davido (Cardinal) on Aug 24, 2004 at 06:10 UTC

    I like File::ReadBackwards' object oriented interface. ...and just for the fun of it, here's a somewhat golfy usage of that interface:

    use File::ReadBackwards; print +( File::ReadBackwards-> new( 't1000f.txt' ) || die $! )->readline();

    Of course, you'll need to alter the filename to whatever file it is that you're trying to read. On my system, t1000f.txt is a newline delimited text file listing the top 1000 most common English words in common speech, sorted in descending order by frequency of use. It turns out that 'neck' is #1000. :)


    Dave

Re: Read Last Line of A File Only
by Plankton (Vicar) on Aug 23, 2004 at 19:16 UTC
    Maybe you could use tail -1
    #!/usr/bin/perl -w use strict; while( 1 ) { my $file = shift || last; open TAIL, "tail -1 $file|" or die "can't do tail -1 $file:$!\ +n"; my $line = <TAIL>; print $line; close TAIL; }

    Plankton: 1% Evil, 99% Hot Gas.

      Or better yet, using the safer syntax options available in 5.6 and later:

      my $lastline = do { open my $pipe, '-|', tail => -1, $file or die "Can't spawn tail: $!\n"; <$pipe>; }; print $lastline;

      I'm not sure why you chose to write your loop that way, either. Why the obfuscatory infinite loop with an abort condition as in this?

      while( 1 ) { my $file = shift || last; # ... }

      It took me a moment to realize that you meant something entirely different than what I assumed at first glance. The natural thing would have been to put the abort condition where you normally put it: in the loop condition.

      while( @ARGV ) { my $file = shift; # ... }

      Of course, that's clearer written as

      for my $file ( @ARGV ) { # ... }

      So we get

      for my $file ( @ARGV ) { my $lastline = do { open my $pipe, '-|', tail => -1, $file or die "Can't spawn tail: $!\n"; <$pipe>; }; print $lastline; }

      I find that significantly clearer despite the changes not changing any of the semantics.

      Makeshifts last the longest.

        Or better yet, using the safer syntax options available in 5.6 and later:
        my $lastline = do { open my $pipe, '-|', tail => -1, $file or die "Can't spawn tail: $!\n"; <$pipe>; }; print $lastline;

        Interesting. Could you please point me to the documentation of this feature? I took a look around perldoc and couldn't find it, so I am surely searching the wrong way

        Thanks in advance

        Ciao!
        --bronto


        The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
        --John M. Dlugosz

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-19 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found