Perl newbie needs help comparing two files that contain IP address to find similarities. I need to match on the first three octets from both files first, then I have to handle a range (e.g., 123.45.67.89-123.89.45.67) - would appreciate any assistance. </readme>
File1
138.63.20.48
63.208.170.231
132.3.0.193
63.208.170.198
63.236.1.136
63.236.1.139
205.161.5.239
The second file looks like this:
File2
Company1-100.45.0.0-100.45.255.255
Company2-227.133.171.0-227.133.173.0
Company3-63.208.170.5-63.208.170.254
Company4-95.214.36.0-95.214.39.255
Company5-35.117.181.0-35.117.181.127
Company6-55.207.128.0-55.207.143.255
Company7-138.63.20.12-138.63.20.95
<readme>
Need to have a script that will match the ip 138.63.20.48 with the company7 info in file 2. Code is started and looks like this:
</readme>
open (IP1,"ipsnew.txt");
open (WHERE,"whereby.txt");
while (<IP1>) {
if (/^(.*?)(\d+\.\d+\.\d+)(.*?)$/) {
$beg_line1{2} = $1;
$ip1 = $2;
# print IP1 "$ip1\n";
# print "@ip1\n";
}
}
while (<WHERE>) {
if (/^(.*?)(\d+\.\d+\.\d+)(.*?)$/) {
$beg_line2 = $1;
$ip2 = $2;
# print IP1 "$ip1\n";
# print "@ip2\n";
}
}
if ($beg_line1{$ip1} == $beg_line2{$ip2}) {
print "we have a match at $ip1\n";}
<readme>
Would appreciate any help
</readme>
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.