read last line only... 
Author Message
 read last line only...

I'm trying to read the last line of a file only, using print, any ideas ?

Thanks,
Diavlo3



Wed, 18 Jun 1902 08:00:00 GMT  
 read last line only...

Quote:

>I'm trying to read the last line of a file only, using print, any ideas ?

You're not going to have much reading lines of any kind with print.

However, if you're asking how to print the last line of a file, then
it's fairly easy:

        #! /usr/local/bin/perl -w

        use strict;

        my $last;

        while (<>)
                { $last = $_; }

        if (defined $last)
                {
                chomp $last;
                print "The last line was '$last'.\n";
                }
        else
                {
                print "There doesn't appear to have been any input.\n";
                }

Hope that helps.

  - Logan



Wed, 18 Jun 1902 08:00:00 GMT  
 read last line only...
On 06 Nov 2000 11:17:47 GMT,

Quote:
> I'm trying to read the last line of a file only, using print, any ideas ?

You don't use print to read a line from a file. You use readline, or the
diamond operator.

If you're on Unix

my $last_line = `tail -1 $file`;

Otherwise:

open(FILE, $file) or die $!;

# If the file is small
my $last_line = (<FILE>)[-1];
# or
my ($last_line) = reverse <FILE>;

# If the file is larger:
my $last_line;
while(<FILE>) { $last_line = $_ }

# If the file is large
use File::ReadBackwards;
my $fbw = File::ReadBackwards->new($file) or die $!;
my $last_line = $fbw->readline;

or you can write stuff with seek and such that does what the
abovementioned module does.

maybe this should be added to the FAQ.

Martien
--
Martien Verbruggen              |
Interactive Media Division      | For heaven's sake, don't TRY to be
Commercial Dynamics Pty. Ltd.   | cynical. It's perfectly easy to be
NSW, Australia                  | cynical.



Wed, 18 Jun 1902 08:00:00 GMT  
 read last line only...
Today around 11:17am, Enjoyer hammered out this masterpiece:

: I'm trying to read the last line of a file only, using print, any ideas ?

Well, you can't really _read_ using print().

Here's one solution:

  print +(<FH>)[-1];

This is of course, dependent on what $/ is set to.

--

'http://home.kiski.net/~crt'};print "\n",'.'x(length($sig->{site})+6),"\n";
print map{$_.': '.$sig->{$_}."\n"}sort{$sig->{$a}cmp$sig->{$b}}keys%{$sig};
my $VERSION = '0.01'; #'patched' by Jerrad Pierce <belg4mit at MIT dot EDU>



Wed, 18 Jun 1902 08:00:00 GMT  
 read last line only...
: I'm trying to read the last line of a file only, using print, any ideas ?

        perl -ne'print if eof' yourfile

Joy,
Yanick

--
($_,$y)=("Yhre lo  .kePnarhtretcae\n",   '(.) (.)'  );

                                     "     `---'    ";



Wed, 18 Jun 1902 08:00:00 GMT  
 read last line only...


  YC> : I'm trying to read the last line of a file only, using print, any ideas ?

  YC>        perl -ne'print if eof' yourfile

use File::ReadBackwards ;

$fh = File::ReadBackwards( 'foo' ) ;

print $fh->readline() ;

uri

--

SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com



Sat, 26 Apr 2003 12:00:10 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. read last line without reading previous lines, how?

2. best way to read last line?

3. Reading Last X Lines of File?

4. Quickly read the last 10 lines of a very large logfile

5. Reading last 3 lines from the file

6. Reading the last line of a file

7. [question: cannot delete the last line of a file]

8. Extract last word on line

9. Extracting last few characters in a line

10. how to read in line by line?

11. How to get last line in ASCII file?

12. trim the last blank-line and compare files

 

 
Powered by phpBB® Forum Software