Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I'm assuming you only need to count the number of hits as you say, not list them as you have done in your example. First you need the string for yesterdays date. This is one way but there are others....
use POSIX qw(strftime); $time= localtime(time-(24*60*60)); $yesterday = strftime( '%d/%m/%y', $time);
There are other and better ways using libraries such as Date::Calc to do the same thing.
`grep "$yesterday" logfile.log | wc -l`

Number of lines returned is the number of hits on your site. This command can be run inside a Perl script inside backticks.

However, the grep command exists in Perl too, so just read the file into an array and use the Perl grep command, as in scalar context it returns the number of matches.

open(LOGFILE,"<", "access.log")or die"Could not open log file."; my @logfile = <LOGFILE> my $hits = grep /$yesterday/, @logfile;
A Monk aims to give answers to those who have none, and to learn from those who know more.

In reply to Re: counting yesterdays hits in a logfile by space_monk
in thread counting yesterdays hits in a logfile by jrp370

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2014-10-25 00:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (138 votes), past polls