Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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
  • Comment on How grep IP-addresses that is an exact match?

Replies are listed 'Best First'.
Re: How grep IP-addresses that is an exact match?
by Fletch (Bishop) on Apr 02, 2004 at 23:02 UTC

    If you want an exact match you either want to anchor your regex somehow (... /,$host$/... probably in this case; something like /(?:^|\D)\Q$host\E(?:\D|$)/ more generally), or split your data and test with eq.

Re: How grep IP-addresses that is an exact match?
by The Mad Hatter (Priest) on Apr 02, 2004 at 23:01 UTC
    Changing grep /$host/, (@ip) to grep /^\w,$host$/, (@ip) should work (by requiring the pattern (the IP) to compose the entire line). Also be aware that . is a regex metacharacter (match anything but \n), and so the line above really should be grep /\w,\Q$host\E$/, (@ip) to escape any metachars in $host.

    Update Didn't see the a, ... b, ... c, ... in ip.txt at first.

Re: How grep IP-addresses that is an exact match?
by BUU (Prior) on Apr 03, 2004 at 06:27 UTC
    If you want an "exacty match" use the "equality operator" also known as "eq" or "==". grep$_==$ip,<FILE>;
Re: How grep IP-addresses that is an exact match?
by wolfi (Scribe) on Apr 03, 2004 at 08:07 UTC

    i believe, i've been doing an almost identical routine lately to what you've got there. When i first began, i kept getting the same results (and frustration), that you've gotten. The others touched on the key for me in their examples, but i thought, i would elaborate on what's going on.

    say, you have... $host = 1.2.3.4
    and you search for /$host/

    you could get back...
    1.2.3.4
    11.2.3.4
    201.2.3.4
    1.2.3.43
    1.2.3.42
    111.2.3.49
    or even... cat1.2.3.4dog

    because perl regexes are so greedy in nature. The keys to changing this behavior for me were the addition of -
    the caret ^ symbol - which is a beginning-anchor ("nothing before")
    and the dollar sign $ - which is an ending-anchor ("nothing after")

    these'll let perl know, you are searching for one specific expression. I use'em almost religiously now in those kinds of pattern matches.

    assuming that each $host(@ip_host) entry is an ip addy in the same format, i would think, that simply grepping /^$host$/ would work.

    either way, hope it works out for you :-)

Re: How grep IP-addresses that is an exact match?
by ambrus (Abbot) on Apr 03, 2004 at 20:07 UTC

    See what \b does in a regular expression.