devin
devin

Reputation: 6537

Grep specific IP addresses

I have an IP address, say 192.168.1.1, that I want to match.

I have a file called afile that looks like this

 item1, item2, 192.168.1.1
 item3, item4, 192.168.1.10
 item5, item6, 192.168.2.1

If I do grep "192.168.1.1" afile both the first and second lines of afile are matched, which is not what I want.

I'm aware of plenty of regular expressions that will match any IP address, but I'm not aware of any regular expressions that match a particular IP address.

Any help with this is appreciated.

What if the IP address isn't on the end of the line? For example, what if columns two and three were swapped in my example file?

Upvotes: 3

Views: 19440

Answers (4)

plzdonotthrow
plzdonotthrow

Reputation: 1

this is what you want:

grep "192.168.1.1\s" afile

Upvotes: 0

Brigand
Brigand

Reputation: 86270

grep is an abbreviated version of g/re/p where "re" is a regular expression. In RegEx, a period means "Any character". To make it a literal period, escape it with a backslash.

grep "192\.168\.1\.1$" afile

As others said, you also need to say it ends the line.

Upvotes: 1

pgl
pgl

Reputation: 8031

You need to add a $ to the end of the regex, to make it match the end of the line. I would also suggest escaping the dots, as a . in a regex means "match any character". So your regex would become:

grep "192\.168\.1\.1$" afile

Upvotes: 8

Kassym Dorsel
Kassym Dorsel

Reputation: 4843

You want to match end of line after the last one. Try this :

grep "192.168.1.1$"

Upvotes: 1

Related Questions