jrp370 has asked for the wisdom of the Perl Monks concerning the following question:
using muba's advice I changed how I was getting the value and format of $yesterday which made pattern matching much simpler
I am working on an assignment where i need to parse a log file and create a website based on said log file. one of the requirements is that i count the number of hits that happened on yesterdays, im lost when it comes to this i feel like i am missing something really simple but i cant see it. ive attached my code and the log file im working with hoping that someone can offer some advice, thanks
#!/usr/bin/perl use strict; use warnings; use Time::Piece; use Time::Seconds qw(ONE_DAY); my $yesterday = = strftime("%d/%b/%Y",localtime(time()-86400)); open(LOGFILE,"<", "access.log")or die"Could not open log file."; my $yesterdayHits=0; my $totalhits=0; my $webPage='log.html'; open(WEBPAGE,">",$webPage); print WEBPAGE ("<HEAD><TITLE>Access Counts</TITLE></HEAD>"); print WEBPAGE ("<BODY>"); print WEBPAGE ("<H1> today is: ",scalar(localtime), "</H1>"); print WEBPAGE ("<h3>Yesterday was $yesterday</h3>"); print WEBPAGE ("<TABLE BORDER=1 CELLPADDING=10 width='500px'>"); foreach my $line (<LOGFILE>) { $totalhits++; my $w = "(.+?)"; $line =~ m/^$w $w $w \[$w:$w $w\] "$w $w $w" $w $w/; my $site = $1; my $logName = $2; my $fullName = $3; my $date = $4; my $time = $5; my $gmt = $6; my $req = $7; my $file = $8; my $proto = $9; my $status = $10; my $length = $11; if($line =~ m/$yesterday/){$yesterdayHits++} print WEBPAGE ("<Tr><TD>$site</TD><TD>$line</TD></Tr>\n\n"); } close(LOGFILE); print WEBPAGE ("<h2>Total hits: $totalhits</h2>"); print WEBPAGE ("<h3>Hits Yesterday: $yesterdayHits</h3>"); print WEBPAGE ("</TABLE></P>"); print WEBPAGE ("</BODY></HTML>"); close(WEBPAGE);
Access log 66.249.65.107 - - [11/Nov/2012:19:33:01 -0400] "GET /support.html +HTTP/1.1" 200 11179 111.111.111.111 - - [11/Nov/2012:19:33:01 -0400] "GET / HTTP/1.1" +200 10801 111.111.111.111 - - [08/Oct/2007:11:17:55 -0400] "GET /style.css H +TTP/1.1" 200 3225 123.123.123.123 - - [26/Apr/2000:00:23:48 -0400] "GET /pics/wpaper +.gif HTTP/1.0" 200 6248 123.123.123.123 - - [26/Apr/2000:00:23:40 -0400] "GET /asctortf/ H +TTP/1.0" 200 8130 123.123.123.123 - - [26/Apr/2000:00:23:48 -0400] "GET /pics/5star2 +000.gif HTTP/1.0" 200 4005 123.123.123.123 - - [26/Apr/2000:00:23:50 -0400] "GET /pics/5star. +gif HTTP/1.0" 200 1031 123.123.123.123 - - [26/Apr/2000:00:23:51 -0400] "GET /pics/a2hlog +o.jpg HTTP/1.0" 200 4282 123.123.123.123 - - [26/Apr/2000:00:23:51 -0400] "GET /cgi-bin/new +count?jafsof3&width=4&font=digital&noshow HTTP/1.0" 200 36 172.16.130.42 - - [26/Apr/2000:00:00:12 -0400] "GET /contacts.html + HTTP/1.0" 200 4595 10.0.1.3 - - [26/Apr/2000:00:17:19 -0400] "GET /news/news.html HTT +P/1.0" 200 16716 129.21.109.81 - - [26/Apr/2000:00:16:12 -0400] "GET /download/wind +ows/asctab31.zip HTTP/1.0" 200 1540096 192.168.198.92 - - [22/Dec/2002:23:08:37 -0400] "GET / HTTP/1.1" 2 +00 6394 192.168.198.92 - - [22/Dec/2002:23:08:38 -0400] "GET /images/logo. +gif HTTP/1.1" 200 807 192.168.72.177 - - [22/Dec/2002:23:32:14 -0400] "GET /news/sports. +html HTTP/1.1" 200 3500 192.168.72.177 - - [22/Dec/2002:23:32:14 -0400] "GET /favicon.ico +HTTP/1.1" 404 1997 192.168.72.177 - - [04/Nov/2012:23:32:15 -0400] "GET /style.css HT +TP/1.1" 200 4138 192.168.72.177 - - [22/Dec/2002:23:32:16 -0400] "GET /js/ads.js HT +TP/1.1" 200 10229 192.168.72.177 - - [22/Dec/2002:23:32:19 -0400] "GET /search.php H +TTP/1.1" 400 1997 127.0.0.1 - - [10/Apr/2007:10:39:11 +0300] "GET / HTTP/1.1" 500 60 +6 127.0.0.1 - - [10/Apr/2007:10:39:11 +0300] "GET /favicon.ico HTTP/ +1.1" 200 766 139.12.0.2 - - [10/Apr/2007:10:40:54 +0300] "GET / HTTP/1.1" 500 6 +12 139.12.0.2 - - [10/Apr/2007:10:40:54 +0300] "GET /favicon.ico HTTP +/1.1" 200 766 127.0.0.1 - - [10/Apr/2007:10:53:10 +0300] "GET / HTTP/1.1" 500 61 +2 127.0.0.1 - - [10/Apr/2007:10:54:08 +0300] "GET / HTTP/1.0" 200 37 +00 127.0.0.1 - - [10/Apr/2007:10:54:08 +0300] "GET /style.css HTTP/1. +1" 200 614 127.0.0.1 - - [10/Apr/2007:10:54:08 +0300] "GET /img/pti-round.jpg + HTTP/1.1" 200 17524 127.0.0.1 - - [10/Apr/2007:10:54:21 +0300] "GET /unix_sysadmin.htm +l HTTP/1.1" 200 3880 217.0.22.3 - - [04/Nov/2012:10:54:51 +0300] "GET / HTTP/1.1" 200 3 +4 217.0.22.3 - - [10/Apr/2007:10:54:51 +0300] "GET /favicon.ico HTTP +/1.1" 200 11514 217.0.22.3 - - [10/Apr/2007:10:54:53 +0300] "GET /cgi/pti.pl HTTP/ +1.1" 500 617 127.0.0.1 - - [10/Apr/2007:10:54:08 +0300] "GET / HTTP/0.9" 200 37 +00 217.0.22.3 - - [10/Apr/2007:10:58:27 +0300] "GET / HTTP/1.1" 200 3 +700 217.0.22.3 - - [10/Apr/2007:10:58:34 +0300] "GET /unix_sysadmin.ht +ml HTTP/1.1" 200 3880 217.0.22.3 - - [10/Apr/2007:10:58:45 +0300] "GET /talks/Fundamenta +ls/read-excel-file.html HTTP/1.1" 404 311
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: counting yesterdays hits in a logfile
by muba (Priest) on Nov 13, 2012 at 03:11 UTC | |
by jrp370 (Initiate) on Nov 13, 2012 at 21:36 UTC | |
|
Re: counting yesterdays hits in a logfile
by Kenosis (Priest) on Nov 13, 2012 at 03:48 UTC | |
|
Re: counting yesterdays hits in a logfile
by space_monk (Chaplain) on Nov 13, 2012 at 05:10 UTC | |
|
Re: counting yesterdays hits in a logfile
by space_monk (Chaplain) on Nov 13, 2012 at 05:29 UTC | |
by Anonymous Monk on Nov 13, 2012 at 06:00 UTC |