Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Match only certain IP Addresses

by monktim (Friar)
on Aug 06, 2003 at 21:14 UTC ( [id://281593]=note: print w/replies, xml ) Need Help??


in reply to Match only certain IP Addresses

Here is a little more than you need. The pattern match grabs everything up to the first space. The keys in the hash contain the unique ip addresses. The values tell you how many times they appeared during processing. This is a classic counting example.
use strict; use warnings; use Socket; my @ip = ( '159.230.1.1 255.255.224.0', '159.230.1.20 255.255.252.0', '158.230.1.3 255.255.255.0', '159.230.1.3 255.255.255.0', '159.230.1.20 255.255.1.1' ); print join ("\n", @ip); print "\n\n"; my %count; foreach (@ip) { $count{inet_aton($&)}++ if /159\.230\..*?( |$)/; } open(FILE, '>ip.txt'); foreach (sort keys %count) { print inet_ntoa($_)." appears $count{$_} times.\n"; print FILE inet_ntoa($_)."\n"; } close FILE
UPDATE: Good point on the sort not working out. I put the inet functions in like other posts did. I reread the question and realized the user only wants 159.230 address so I changed the pattern match from /.*? /. That change will also get rid of the leading text if it exists. Of course the \d{1,3} match the other poster mentioned does a little more verification on your data if you decide you need it.
Good luck.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-19 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found