Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

[SOLVED] Splitting a file using "common log format" help

by jaffinito34 (Acolyte)
on Nov 10, 2012 at 19:55 UTC ( [id://1003274]=perlquestion: print w/replies, xml ) Need Help??

jaffinito34 has asked for the wisdom of the Perl Monks concerning the following question:

My script is attempting to read a log file with common log format. An example of the file is below:

66.249.65.107 - - [08/Oct/2007:04:54:20 -0400] "GET /support.html HTTP +/1.1" 200 11179

To read the file, I used a while loop and split is using spaces:

#opening log file open (LOGG, '<', 'IN-access.log'); #reading log file and assigning values while ($lines = <LOGG>){ ($remoteIP,$rfc,$userID,$dateTime,$requestType,$statusCode,$sizeOf +File) = split ' ', $lines; print "$statusCode $sizeOfFile\n"; }

However, the split function is actually splitting the spaces inside the data (ie 08/Oct/2007:04:54:20 -0400 <-- this is supposed to be the dateTime variable but because there's a space, its actually two variables according to the script) Any help or reference to help is appreciated.

Replies are listed 'Best First'.
Re: Splitting a file using "common log format" help
by toolic (Bishop) on Nov 10, 2012 at 21:08 UTC
    You could separate out the datetime section using s/// before you split:
    use warnings; use strict; while (<DATA>) { if (s/(\[.*])//) { my $dateTime = $1; my ($remoteIP,$rfc,$userID,$requestType,$statusCode,$sizeOfFil +e) = split; print "$statusCode $sizeOfFile\n"; } } __DATA__ 66.249.65.107 - - [08/Oct/2007:04:54:20 -0400] "GET /support.html HTTP +/1.1" 200 11179
Re: [SOLVED] Splitting a file using "common log format" help
by trizen (Hermit) on Nov 10, 2012 at 23:20 UTC
    Using Text::ParseWords.
    use strict; use warnings; use Text::ParseWords; while (<DATA>) { if (s/(\[.*?\])//) { my $dateTime = $1; my ($remoteIP,$rfc,$userID,$requestType,$statusCode,$sizeOfFil +e) = quotewords(qr/\s+/, 0, $_); print "<$remoteIP> <$rfc> <$userID> <$requestType> <$statusCod +e> <$sizeOfFile>\n"; } } __DATA__ 66.249.65.107 - - [08/Oct/2007:04:54:20 -0400] "GET /support.html HTTP +/1.1" 200 11179

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found