Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to read last lines from file?

by httpd (Novice)
on Nov 02, 2011 at 12:56 UTC ( #935383=perlquestion: print w/ replies, xml ) Need Help??
httpd has asked for the wisdom of the Perl Monks concerning the following question:

Hello guys, I am writing a log parser script, i've got the problemm please look: I want to print the lines from big file that were added for last 5 minutes my scenario: 1. read the file from the end untill date in file < current date on 300 secs:

# tac /var/log/biglog | perl -ne 'print; exit 0 if (($DateCurrent -$D +ate) > 300 );'

where $DateCurrent - current date in unix timestamp: date +%s $Date - the date from log file, but the problem here is that it has format that I cannot convert to unixtimestamp for compare:

# tail -3 biglog 78.49.150.102 - - [01/Nov/2011:00:53:01 +0600] "GET /mem.png HTTP/1.1" + 200 51883 "http://net.com/" "Opera/9.80 (Windows NT 6.1; U; ru) Pres +to/2.9.168 Version/11.52" 78.49.150.102 - - [01/Nov/2011:00:53:01 +0600] "GET /cpu.png HTTP/1.1" + 200 42875 "http://net.com/" "Opera/9.80 (Windows NT 6.1; U; ru) Pres +to/2.9.168 Version/11.52" 78.49.150.102 - - [01/Nov/2011:00:53:01 +0600] "GET /traf.png HTTP/1.1 +" 200 48121 "http://net.com/" "Opera/9.80 (Windows NT 6.1; U; ru) Pre +sto/2.9.168 Version/11.52"
any ideas?

Original content restored above by GrandFather

thanks, guys!

Comment on How to read last lines from file?
Select or Download Code
Re: How to read last lines from file?
by moritz (Cardinal) on Nov 02, 2011 at 13:12 UTC
Re: How to read last lines from file?
by keszler (Priest) on Nov 02, 2011 at 13:14 UTC
    >perl -E "use Date::Parse;say scalar gmtime str2time('01/Nov/2011:00:5 +3:01 +0600');" Mon Oct 31 18:53:01 2011
Re: How to read last lines from file?
by jethro (Monsignor) on Nov 02, 2011 at 13:15 UTC

    How about using Time::Local to convert the date to unix timestamp.

Re: How to read last lines from file?
by zentara (Archbishop) on Nov 02, 2011 at 17:04 UTC
    #!/usr/bin/perl -w use strict; use File::Tail; my $log = "/usr/local/apache2/logs/access_log"; my $file=File::Tail->new ( name=>$log, interval=>2, maxinterval=>10 ); while (defined(my $line=$file->read)) { print "$line"; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-08-01 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (0 votes), past polls