You just need to make sure to handle the IP-range issues properly -- if I understand the question, a File2 entry like "Company6-55.207.128.0-55.207.143.255" would be a hit for any File1 IP whose third component falls between 128 and 143. Something like this could get you started:
(not tested)use strict; my %ip_company; open( I, "File1" ) or die "File1: $!"; while (<I>) { chomp; my ( $company, $bgn_IP, $end_IP ) = split /-/; next unless ( $bgn_IP =~ /^(\d+\.\d+\.)(\d+)\.\d+$/ ); my ( $bgn_q12, $bgn_q3 ) = ( $1, $2 ); next unless ( $end_IP =~ /^(\d+\.\d+\.)(\d+)\.\d+$/ ); my ( $end_q12, $end_q3 ) = ( $1, $2 ); # NB: if $bgn_q12 ne $end_q12, we need some different logic... $ip_company{$bgn_q12.$bgn_q3} = $company; if ( $bgn_q3 != $end_q3 ) { for my $next_q3 ( $bgn_q3+1 .. $end_q3 ) { $ip_company{$bgn_a12.$next_q3} = $company; } } } open( I, "File2" ) or die "File2: $!"; while (<I>) { chomp; ( my $lookup = $_ ) =~ s/\.\d+$//; if ( exists( $ip_company{$lookup} )) { print "$_ is part of $ip_company{$lookup}\n"; } else { print "$_ is not part of any known company\n"; } }
Handling a range like "123.45.67.89-123.89.45.67" is left as an exercise... (or maybe you do don't have to go there).
(update: fixed last sentence so it makes sense)
In reply to Re: Compare two files (Ip addresses)
by graff
in thread Compare two files (Ip addresses)
by jorain
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |