-----Original
Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]On Behalf Of Márcio Oliveira Sent: Tuesday, July 27, 2004 09:13 To: [EMAIL PROTECTED] Subject: [Perl-unix-users] Simple question about split string Hi all.
I have a simple (I guess) question
about spliting strings in perl.
I need to split a string like this:
"10.10.2.131 - - [27/Jul/2004:08:20:43 -0300] "GET http://lalala/chiclete_01.jpg HTTP/1.0"
407 1399 TCP_DENIED:NONE" (a squid log) in some parts:
$ip = 10.10.1.131
$date = [27/Jul/2004:08:20:43
-0300]
$url_info = http://lalala/chiclete_01.jpg
$state = TCP_DENIED:NONE
How i can extract this parts
from a var eq that string?
This is the script I use to read the file and get
the string ($linha) one per line:
#!/usr/bin/perl
use strict; my ($linha);
open (FILE,"<access.log"); while ( defined ($linha = <FILE>) ) {
chomp $linha; #???? what i can use ?
Here is a start. I use the ! to let me know that I failed as part
of the regex, but you may just want to write to another log file for you to peruse and make cahnges to your regex to capture them. Like I say, it is a
starting point only.
use strict;
use warnings; $_ = '10.10.2.131 - - [27/Jul/2004:08:20:43 -0300] "GET http://lalala/chiclete_01.jpg HTTP/1.0" 407 1399
TCP_DENIED:NONE';
if ( ! /^(\S+).+\[([^\]]+)\]\s+"get\s+([^"]+)"\s+\d+\s+\d+\s+(\S+)/i )
{
printf "Expecting a hit, but did not recieve on on this data.\n"; printf "Data:\n<%s>\n", $_; die "Verify regex and try again" } my $MyIpAddr = $1; my $MyTime = $2; my $MyURL = $3; my $MyMsg = $4; printf "IP Addr: %s\nTime: %s\nUrl: %s\nMsg:
%s\n",
$MyIpAddr, $MyTime, $MyURL, $MyMsg; Output:
IP Addr: 10.10.2.131
Time: 27/Jul/2004:08:20:43 -0300 Url: http://lalala/chiclete_01.jpg HTTP/1.0 Msg: TCP_DENIED:NONE where
/^(\S+).+\[([^\]]+)\]\s+"get\s+([^"]+)"\s+\d+\s+\d+\s+(.+)/i
^ -
start of variable
(\S+) - Capture
NON Space
.+\[ - skip until you hit
[
[([^\]]+) - capture until you hit a
]
]\s+"get\s+ - skip by the
get
([^"]+) - cpature untilyou hit
" Note: This will have the HTTP/1.0. Uncertain if it can be
aything else
\s+\d+\s+\d+\s+ - space(s) Number(s)
Space(s) Number(s)
(.+) - Capture to end of
variable
Wags ;)
} ******************************************************* This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. ******************************************************* |
_______________________________________________ Perl-Unix-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs