I'm trying to search a large list of IP address for a particular IP, but am getting false positives because of spaces. Please consider the following example:
$ a="192.168.1.1 192.168.1.2"
$ ip="1.1.192.168"
$ echo $a | grep "$ip"
192.168.1.1 192.168.1.2
Grep ignores the space and returns wrong results. How can I get grep to recognize that "1.1 192.168" is not equal to "1.1.192.168"?
.
is a special grep operator which means "any character". Usegrep -Fw "$ip"
and go read grep's manpage.192.168.1.2
.1
,2
,6
,8
,9
,192
,168
, and.
(any character) all have matches in the output from theecho
command. If you just want it to return. If you want to look for that exact string, then as mosvy said, you need to usegrep -F
orgrep -w
.1<any char>1<any char>192<any char>168
andecho $a
contains1.1 192.168
. The output ofecho $a
is different fromecho "$a"
.