Question

I need to be able to search through a log and grab IP addresses that were logged. An example log line would look like:

[2012-06-05 11:59:52] NOTICE[14369] chan_sip.c: Registration from '' failed for 'yy.yy.yy.yyy' - No matching peer found

I need to grab the IP address listed in the yy.yy.yy.yyy position. With other log files, the yy.yy.yy.yyy would be in a different position.

I was thinking to read each line, split on ' ' and then loop through the split temporary array for: 'yy.yy.yy.yyy'. I just don't know how to pattern match or regex for 'yy.yy.yy.yyy' with the single quotes included. How can I do this?

Était-ce utile?

La solution

This regex will match your ip address contained in '

'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'

To iterate over all the matches in perl do:

while ($subject =~ m/'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'/g) {
    # matched text = $&
}

Group 1 of the match will only contain the IP so without the '

Autres conseils

while (<>) {
    my ($ip) = $line =~ /for '((?:[0-9]+\.){3}[0-9]+)'/
       or next;

    ... do something with $ip ...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top