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

In reply to Re: can't read the 2nd input file by tybalt89
in thread can't read the 2nd input file by perl_boy

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.