#!perl # use strict ; use warnings ; open INFO, 'IPs.txt' or die $! ; chomp( my @IPs = ) ; close INFO ; my @rows = () ; open IN, ' source.txt' or die $! ; while () { next if /^$/ ; my %table = () ; @table{ 'ip', 'ip2', 'country', 'region' } = split ; $table{ 'num_ip' } = sprintf '%03d' x 4, split /\./, $table{ 'ip' } ; $table{ 'num_ip2' } = sprintf '%03d' x 4, split /\./, $table{ 'ip2' } ; push @rows, \%table ; } close IN; open OUT, '>>conversion.txt' or die $! ; foreach my $ip ( @IPs ) { my $match = 0 ; my $num_ip = sprintf '%03d' x 4, split /\./, $ip ; foreach my $row ( @rows ) { if ( $num_ip >= $row->{'num_ip'} && $num_ip <= $row->{'num_ip2'} ) { printf OUT "%-16s %s\n", $ip, $row->{'region'} ; $match++ ; } } printf OUT "%-16s\n", $ip if !$match ; } close OUT;