datafile.html contains (in part):#!/usr/local/bin/perl -slw use strict; use Date::Manip; use Data::Dumper; my $now = ParseDate( scalar localtime()); print "now is $now<p>"; my $then = DateCalc( $now, ParseDateDelta( "7 hours 48 minutes ago" )) +; my $err; open LOGFILE, "datafile.html" || die "Can't open file"; my $re = qr/ ^.*? # Skip the first part \[([^\]]+)\]\s+ # capture everything between [] "[^"]+"\s+ # skip a quoted string and whitespace .*? # and a couple of numbers or blanks "( [^"]+ )" # capture the next quoted string /x; my %referrers; while(<LOGFILE>) { my @chunks = /$re/; my $ts = ParseDate $chunks[0]; print "The line '@chunks' was logged ", Delta_Format( DateCalc( $ts, $now, \$err ), 2, ("%mt")), " minutes ago."; if ( Date_Cmp( $ts, $then ) > 0 and Date_Cmp( $ts, $now ) < 0 ) { print "The previous line is within the window. Counting..."; $referrers{$chunks[1]}++; } else { print "Discarding previous line"; } } print "\nThese are the referrers counted:\n", Dumper(\%referrers);
68.22.179.211 - - [15/Dec/2002:14:52:12 -0500] "GET /scripts/s_code.js HTTP/1.1"
304 - "http://www.indystar.com/print/articles/6/008596-6466-040.html" "Mozilla/
4.0 (compatible; MSIE 5.5; Windows 98)"
152.163.188.37 - - [15/Dec/2002:14:52:12 -0500] "GET /icons/unknown.gif HTTP/1.1
" 200 245 "http://www.indystar.com/print/articles/?S=D" "Mozilla/4.0 (compatible
; MSIE 5.5; AOL 7.0; Windows 98)"
68.22.179.211 - - [15/Dec/2002:14:52:12 -0500] "GET /images/white_159x60.gif HTT
P/1.1" 304 - "http://www.indystar.com/print/articles/6/008596-6466-040.html" "Mo
zilla/4.0 (compatible; MSIE 5.5; Windows 98)"
141.154.123.193 - - [15/Dec/2002:14:52:13 -0500] "GET /print/articles/2/008227-9
652-031.html HTTP/1.0" 200 7275 "http://www.fark.com/" "Mozilla/4.79 [en] (Windo
ws NT 5.0; U)"
68.22.179.211 - - [15/Dec/2002:14:52:13 -0500] "GET /images/black_1x60.gif HTTP/
1.1" 304 - "http://www.indystar.com/print/articles/6/008596-6466-040.html" "Mozi
lla/4.0 (compatible; MSIE 5.5; Windows 98)"
68.22.179.211 - - [15/Dec/2002:14:52:13 -0500] "GET /images/69.gif HTTP/1.1" 200
1348 "http://www.indystar.com/print/articles/6/008596-6466-040.html" "Mozilla/4
.0 (compatible; MSIE 5.5; Windows 98)"
141.154.123.193 - - [15/Dec/2002:14:52:13 -0500] "GET /images/header_aod2_01.gif
HTTP/1.0" 200 2011 "http://www.indystar.com/print/articles/2/008227-9652-031.ht
ml" "Mozilla/4.79 [en] (Windows NT 5.0; U)"
141.154.123.193 - - [15/Dec/2002:14:52:13 -0500] "GET /images/header_aod2_15.gif
HTTP/1.0" 200 4162 "http://www.indystar.com/print/articles/2/008227-9652-031.ht
ml" "Mozilla/4.79 [en] (Windows NT 5.0; U)"
141.154.123.193 - - [15/Dec/2002:14:52:13 -0500] "GET /images/header_aod2_10.gif
HTTP/1.0" 200 3034 "http://www.indystar.com/print/articles/2/008227-9652-031.ht
ml" "Mozilla/4.79 [en] (Windows NT 5.0; U)"
141.154.123.193 - - [15/Dec/2002:14:52:13 -0500] "GET /images/go_blue.gif HTTP/1
.0" 200 133 "http://www.indystar.com/print/articles/2/008227-9652-031.html" "Moz
illa/4.79 [en] (Windows NT 5.0; U)"
141.154.123.193 - - [15/Dec/2002:14:52:13 -0500] "GET /images/aod_searchend2.gif
HTTP/1.0" 200 186 "http://www.indystar.com/print/articles/2/008227-9652-031.htm
l" "Mozilla/4.79 [en] (Windows NT 5.0; U)"
24.79.125.220 - - [15/Dec/2002:14:52:13 -0500] "GET /images/coheader2_aod_08.gif
HTTP/1.1" 304 - "http://www.indystar.com/forums/showthread.php?s=&postid=177044
" "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Q312461)"
24.79.125.220 - - [15/Dec/2002:14:52:13 -0500] "GET /images/coheader2_aod_10.gif
HTTP/1.1" 304 - "http://www.indystar.com/forums/showthread.php?s=&postid=177044
" "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Q312461)"
141.154.123.193 - - [15/Dec/2002:14:52:13 -0500] "GET /images/email.gif HTTP/1.0
" 200 138 "http://www.indystar.com/print/articles/2/008227-9652-031.html" "Mozil
la/4.79 [en] (Windows NT 5.0; U)"
66.149.178.96 - - [15/Dec/2002:14:52:14 -0500] "GET /forums/showthread.php?s=&po
stid=177042 HTTP/1.1" 200 7302 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1
.0.1) Gecko/20021003"
24.79.125.220 - - [15/Dec/2002:14:52:14 -0500] "GET /images/coheader2_aod_11.gif
HTTP/1.1" 200 954 "http://www.indystar.com/forums/showthread.php?s=&postid=1770
44" "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Q312461)"
Edit: Added <code> tags. Escaped [s and ]s. larsen
In reply to Re: Re: pulling by regex
by mkent
in thread pulling by regex
by mkent
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |