NOTE: requires portfile to be in sorted order
NOTE: The IPfile read should be in the body of the "while", not in the condition.
NOTE: "goto" not required.
NOTE: Does not even have to read all the way through the IPfile if no IPs that are near the end are referenced.
#!/usr/bin/perl
use strict; # https://perlmonks.org/?node_id=11129698
use warnings;
@ARGV = qw( port.file ip.file out.file); # FIXME only for testing
@ARGV == 3 or die "wrong number of arguments";
open $_, '<', shift or die for my( $F0, $F1);
open $_, '>', shift or die for my $F2;
my ($no, $IP) = 0;
while( <$F0> ) # NOTE assumes port file is in sorted order
{
my ($stop, $port) = /^(\d+)\s+(.*\n)/;
$no += ($IP = <$F1> // die) =~ /^(?:\d+\.){3}\d+\s+\z/ while $no < $
+stop;
print $F2 $IP =~ s/\s+\z//r, "\t$port";
}
close $F2;
system 'cat out.file'; # FIXME only for testing
Outputs (for your larger example with blank lines in the IP file):
1.0.0.29 80/tcp closed http
1.0.0.29 443/tcp closed https
1.0.0.29 8080/tcp open http-proxy
1.0.0.49 80/tcp open http
1.0.0.49 443/tcp filtered https
1.0.0.49 8080/tcp filtered http-proxy
1.0.0.69 80/tcp open http
1.0.0.69 443/tcp filtered https
1.0.0.69 8080/tcp filtered http-proxy