Hello.
How can I grep IP-addresses from a file "ip.txt" and only get the IP that is an exact match?.

"ip_host.txt" is the file with the IP that I want to grep from the file "ip.txt".
I want 4 matches, not 9 matches.

ip_host.txt
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.55



ip.txt
a,192.168.1.1
b,192.168.1.2
c,192.168.1.14
d,192.168.1.3
e,192.168.1.15
f,192.168.1.20
g,192.168.1.19
h,192.168.1.22
i,192.168.1.55
j,192.168.1.59
k,192.168.3.15



The code:
$file = "ip.txt";
open(LOG, $file);
chomp
(@ip = <LOG>);
close LOG;


$file_host = "ip_host.txt";
open(LOG, $file_host);
chomp
(@ip_host = <LOG>);
close LOG;


foreach $host (@ip_host) {

@grep = grep /$host/, (@ip);

foreach $x (@grep)
{
print "$x\n";
}
}



//Anders Andersson

In reply to How grep IP-addresses that is an exact match? by Anonymous Monk

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.