ccrash has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Need advice on PERL
by kabeldag (Hermit) on Dec 12, 2006 at 08:59 UTC | |
- \d is looking for a digit character - + is find \d 1 or more times - (...) Groups subexpressions for capturing to $1, $2,$3 ... In this case, $scrip will return the the complete sub expression match (which is inside the brackets : (\d+\.\d+\.\d+\.\d+) ): INPUT C:\Perl\bin>perl bleh.pl 1.0.3.3_1.3.45.44 OUTPUT $scrip returned : 1.0.3.3 match 1 : 1.0.3.3 match 2 : match 3 : match 4 : If "\d+\.\d+\.\d+\.\d+" wasn't inside the brackets, $scrip would return 1, if it matched the reg-ex: OUTPUT EXAMPLES: C:\Perl\bin>perl bleh.pl 13.3.3.3_ matched 13.3.3.3_ ! $scrip = 1 C:\Perl\bin>perl bleh.pl 13.3. did not match 13.3. ! $scrip = | [reply] [d/l] [select] |
|
Re: Need advice on PERL
by lin0 (Curate) on Dec 12, 2006 at 13:33 UTC | |
Hi ccrash, Here's the code. But I don't know what it means.I only understand that it is checking whether the entry would have something like IP Address as above. But does it pass the IP address to the $srcip variables ? Yes, it does. Your code is using a regular expression to find a pattern that looks like an IP Address. Before telling you how it does it, I recommend you to have a look at the perl documentation on regular expressions. In your particular case, the variable $whole_event_string holds the Log entries. Every Log entry is analysed to see whether it has a pattern that begins (that is the meaning of the ^ symbol) with the following sequence of characters:
if there is a match, that sequence of characters is assigned to the variable $srcip The following code illustrates what I just described:
If you try it, the output should be: $srcip = 1.2.3.4 $srcip = 2.3.4.5 $srcip = 3.4.5.6 $srcip = 4.5.6.7
I hope this helps lin0 | [reply] [d/l] [select] |
|
Re: Need advice on PERL
by jonadab (Parson) on Dec 12, 2006 at 13:56 UTC | |
Others have explained how the pattern match itself works, but they forgot to explain how the whole thing is parsed. The =~ pattern match operator binds more tightly than the = assignment operator, so the pattern match happens first. The parentheses on the left side cause the results of the pattern match to be taken in list context, returning a list of the things captured in parentheses during the match. (This is different from what happens in scalar context. Context is very important in Perl.) In this case it's a list of one thing, which looks like an IPv4 address. That list is assigned to a list of variables. In this case it's a list of just one variable, $srcip. If you wanted to get the four numbers out of the dotted quad, you could do it like this:
Then the array @ipnums would have four entries in it, one for each of the four numbers in the dotted quad. Similarly, if you want to capture more information than just the IP address, you could add to your regular expression and parse more fields with something along these lines...
HTH.HAND. That regular expression may not be exactly right, because I'm not sure of the exact technical specs of the logfile format you're parsing (Is that an IIS log? yuck!), but it illustrates the principle anyway. Also note that if it _is_ an IIS log, or anything else remotely common, there's probably a module on the CPAN for parsing it, although I don't happen to know of a specific module for that, and a quick search didn't turn up anything obvious. Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. You can just call me "Mister Sanity". Why, I've got so much sanity it's driving me crazy. | [reply] [d/l] [select] |
|
Re: Need advice on PERL
by Anonymous Monk on Dec 12, 2006 at 08:52 UTC | |
| [reply] [d/l] |