Reading Last X Lines of File? 
Author Message
 Reading Last X Lines of File?

Is there an easy/straight forward way to read the last X lines
(from 1-4) lines of a text file?

I can read the last line by:

while (<>) {
 $LastLine = $_;

Quote:
}

but how do I capture the last 2-4 lines?

Thanks in advance

ah.

Andrew Hunt




Sun, 25 Aug 1996 14:22:59 GMT  
 Reading Last X Lines of File?
   Is there an easy/straight forward way to read the last X lines
   (from 1-4) lines of a text file?

   I can read the last line by:

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

   but how do I capture the last 2-4 lines?

How about:

open(LASTLINES, "tail -4 $file") || die "Can't open file: $!\n";

while (<LASTLINES>) {
    # do whatever to your file lines

Quote:
}

or you can slurp the file into an array (means you can work with other
parts also....

while (<>) {

Quote:
}


This last one is probably only good if you want to use the other lines for
something, or if it's a relatively small file.  If the file is of any
noticable size, you might prefer the first method.


--
  University of Florida, Department of Computer and Information Sciences

              smail: Stephen P Potter, E309A CSE, UF Campus
Click here to find out about the <a href="http://www.cis.ufl.edu/perl">Perl World Wide Web Page</a>



Sun, 25 Aug 1996 22:39:28 GMT  
 Reading Last X Lines of File?


:    Is there an easy/straight forward way to read the last X lines
:    (from 1-4) lines of a text file?

Would this do it?

while ( <> ) {


Quote:
}


--
--
jan b schipmolder



Mon, 26 Aug 1996 02:23:04 GMT  
 Reading Last X Lines of File?

Quote:


>:    Is there an easy/straight forward way to read the last X lines
>:    (from 1-4) lines of a text file?

>Would this do it?

>while ( <> ) {


>}

>--
>--
>jan b schipmolder


How about:

tail -4 file

---
          __            
         /  \           |
         \__/__         |         Bob Mackay
       ______  \        |         Tandem Computers Inc.
      /   /  \  \       |         (408) 285 4218

       \_______/        |



Mon, 26 Aug 1996 04:34:56 GMT  
 Reading Last X Lines of File?

Quote:

>Is there an easy/straight forward way to read the last X lines
>(from 1-4) lines of a text file?

>I can read the last line by:

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

>but how do I capture the last 2-4 lines?

You can do it without slurping the whole file like this:

#!/usr/local/bin/perl
# usage - mytail filename number
$name = shift;
$num = shift;
open(YOURFILE,"<$name");
$lines[$i++]=(-s $name);
# scan once thru the file to get the length of each line
while(<YOURFILE>){
  $cur+=length;
  $lines[$i++]=($lines[0] - $cur);

Quote:
}

# now fp is at eof, seek to the desired line, read, and spew
seek(YOURFILE,-($lines[$i-($num+1)]),1);        
read(YOURFILE,$output,$lines[$i-($num+1)]);    
print $output;      

This can be generalized to most any separator.

Bill

--

Former Compuinfobankingshoppingtainmentmeister
will hack perl for Texas BBQ



Mon, 26 Aug 1996 05:39:41 GMT  
 Reading Last X Lines of File?

: You can do it without slurping the whole file like this:

: #!/usr/local/bin/perl
: # usage - mytail filename number
: $name = shift;
: $num = shift;
: open(YOURFILE,"<$name");
: $lines[$i++]=(-s $name);
: # scan once thru the file to get the length of each line
: while(<YOURFILE>){
:   $cur+=length;
:   $lines[$i++]=($lines[0] - $cur);
: }
: # now fp is at eof, seek to the desired line, read, and spew
: seek(YOURFILE,-($lines[$i-($num+1)]),1);        
: read(YOURFILE,$output,$lines[$i-($num+1)]);    
: print $output;      

this is handy... but is there a way to follow the file similar to what
tail -f does?

i.e keep the file open and print new lines as they come in?

thanks

Bry

--

bryan collins

                email:

        I just wanna be of the piglet way.................



Wed, 04 Sep 1996 11:43:42 GMT  
 Reading Last X Lines of File?

:
:this is handy... but is there a way to follow the file similar to what
:tail -f does?
:
:i.e keep the file open and print new lines as they come in?

2.41) How do I do a "tail -f" in Perl?

    Larry says that the solution is to put a call to seek in yourself.
    First try

            seek(GWFILE, 0, 1);

    If that doesn't work (depends on your stdio implementation), then
    you need something more like this:

    for(;;) {
            for ($curpos = tell(GWFILE); $_ = <GWFILE>; $curpos = tell(GWFILE)) {
                   search for some stuff and put it into files
            }
            sleep for a while
            seek(GWFILE, $curpos, 0);
    }

--tom
--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212



Thu, 05 Sep 1996 01:08:50 GMT  
 
 [ 7 post ] 

 Relevant Pages 

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

2. Reading last 3 lines from the file

3. Reading the last line of a file

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

5. read last line only...

6. best way to read last line?

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

8. How to get last line in ASCII file?

9. trim the last blank-line and compare files

10. killingthe last line of the file

11. More Efficient Way To Get Last Line From A TXT File

12. [Fwd: How can I get the last element on each line in a text file]

 

 
Powered by phpBB® Forum Software