I think it would make more sense to read File2 first, to get the labels that need to be associated with various IP ranges. If the known IP values are treated as hash keys (and company names are the hash values), then it becomes very simple to look up the addresses in File1, and spit out the company name when there's a match.

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:

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"; } }
(not tested)

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.