in reply to Match only certain IP Addresses
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.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
|
|---|