Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Performing a tail(1) in Perl (reading the last N lines of a file)

by grinder (Bishop)
on Apr 25, 2002 at 16:56 UTC ( [id://162034]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my $limit = 100;
    my $file  = 'big.file';
    
    ...
        return @{$current == \@alpha ? \@beta : \@alpha}[-($limit - scalar
    + @$current)..-1],
            @$current;
    }
    
  2. or download this
    % perl tailbm -30 10 100.lines 
    
    ...
       grinder:  74 wallclock secs (28.38 usr +  2.77 sys = 31.15 CPU) @ 5
    +66.52/s (n=17647)
         lastn:  69 wallclock secs (19.78 usr + 11.34 sys = 31.12 CPU) @ 2
    +2.88/s (n=712)
    lastn_getc:  77 wallclock secs (18.88 usr + 13.17 sys = 32.05 CPU) @ 1
    +9.78/s (n=634)
    
  3. or download this
    % perl tailbm -30 100 721994.lines 
    Benchmark: running f_rb_obj, f_rb_obj_u, f_rb_tie, f_rb_tie_u, file_ta
    +il, grinder, each for at least 30 CPU seconds...
    ...
                (warning: too few iterations for a reliable count)
         lastn:  40 wallclock secs (20.15 usr + 10.80 sys = 30.95 CPU) @ 1
    +9.48/s (n=603)
    lastn_getc:  42 wallclock secs (18.05 usr + 13.81 sys = 31.86 CPU) @ 1
    +7.70/s (n=564)
    
  4. or download this
    #! /usr/bin/perl -w
    
    ...
        'lastn_getc' => \&lastn_getc, 
    });
    __END__
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://162034]
Approved by broquaint
Front-paged by Dominus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found