CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Spliting access_log line into different arays

 



voshka
stranger

Nov 16, 2001, 9:22 AM

Post #1 of 3 (924 views)
Spliting access_log line into different arays Can't Post

Does anyone know how to split a line of a access_log file?

each value is not seporated by a special character, or /t...

it would be so nice if it would be like the passwd file...

open(PASSWD, '/etc/passwd');
while (<PASSWD>) {
chomp;
($login, $passwd, $uid, $gid,
$gcos, $home, $shell) = split(/:/);
#...
}

---- but the bloody access_log file is:
64.210.196.195 - - [07/Nov/2001:20:45:40 +0000] "GET / HTTP/1.0" 200 3518 "http://prospective.co.uk/" "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)"

first is IP assress, then -- then [time] then "get " etc ...

how to seporate it?


what I want to do is to seporate each line's values into seporate arays
e.g. @refferers @addressed @useragents
and then analysze the results.





vallely
Novice

Nov 16, 2001, 7:00 PM

Post #2 of 3 (919 views)
Re: Spliting access_log line into different arays [In reply to] Can't Post

Hi Voshka.
This is one way:
open(FILE,'log.log') || die("Can't open log.log $!\n");

@lines = <FILE>;

close(FILE);

foreach (@lines) {

@aa = split(/ - - /, $_);
push (@ips, $aa[0]);

($date, $get, $addressed, $referrer, $blank, $useragent) = split (/\"/, $aa[1]);


push (@dates, $date);
push (@gets, $get);
push (@addresseds, $addressed);
push (@referrers, $referrer);
push (@useragents, $useragent);
}







voshka
stranger

Nov 17, 2001, 1:38 AM

Post #3 of 3 (917 views)
Re: Spliting access_log line into different arays [In reply to] Can't Post

Thanks!

it works :)



my ($logpath) = 'F:/My software/Perl/log analyzer/';
my ($logfile) = 'access_log';

###################################################################


# open log file for reading:
open (TEMP, "$logpath$logfile") || die ("Cannot Open File $logfile, $!\n");
my (@lines) = <TEMP>;
close(TEMP);

# seporate elements into different variables.

my (@aa, @ips, @dates, @gets, @addresses, @useragents, @referrers);
my ($date, $get, $addressed, $referrer, $blank, $useragent);

# cleaning subs

sub cleandate
{
my ($c_d) = shift;
$c_d =~ tr/[//d;
$c_d =~ tr/+//d;
$c_d =~ s/0000] //g;
$c_d =~ tr/ //d;
return $c_d;
}


foreach (@lines)
{
(@aa) = split(/ - - /, $_);
push (@ips, $aa[0]);
($date, $get, $addressed, $referrer, $blank, $useragent) = split (/\"/, $aa[1]);
push (@dates, &cleandate ($date));
push (@gets, $get);
push (@addresses, $addressed);
push (@referrers, $referrer);
push (@useragents, $useragent);
}
# debug.

print "There are $#lines entries in the log file.\n";
print "There are $#ips ips\n";
print "There are $#dates dates\n";
print "There are $#gets gets\n";
print "There are $#addresses addresess\n";
print "There are $#referrers referrers\n";
print "There are $#useragents useragents\n";
print "\n\n";

print "The seporation in practice:\n\n";
my ($ran) = rand ($#lines);
print "Original:\n$lines[$ran]\n";

print "IP\t\t: [$ips[$ran]]\n";
print "Date\t: [$dates[$ran]]\n";
print "Get\t\t: [$gets[$ran]]\n";
print "Addr\t: [$addresses[$ran]]\n";
print "Ref\t\t: [$referrers[$ran]]\n";
print "Usr\t\t: [$useragents[$ran]]\n";


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives