0

I'm trying to parse the firewall log file and only take the lines that don't contain the router's address as source. The router's address is the obvious 192.168.2.1 and the computer's address is 192.168.2.110.

If I write grep -v 192.168.2.1 then I don't get the destination 192.168.2.110, because it starts with 192.168.2.1. If I don't use anything, then I get the lines from the router, that I would like to filter out. I have searched and tried different regexs, but no matter what I do, I either get both addresses or none.

Andyc
  • 113
  • 4
  • Or [Whole-word matching on a body of text, given a list of words](http://stackoverflow.com/questions/30470371/whole-word-matching-on-a-body-of-text-given-a-list-of-words?noredirect=1&lq=1) – Benjamin W. Sep 13 '16 at 13:37

2 Answers2

1

This force PATTERN to match only whole words grep -w.

grep -v -w 192.168.2.1 file
192.168.2.110

Or Enclose your pattern with \<pattern\>

grep -v  '\<192.168.2.1\>' file
192.168.2.110
P....
  • 17,421
  • 2
  • 32
  • 52
0

You can try to use \b which matches with word boundaries:

grep -vP '\b192.168.2.1\b'

or better yet

grep -vP '\b192\.168\.2\.1\b'

You need the -P mode for this to work.

redneb
  • 21,794
  • 6
  • 42
  • 54
  • These two also work great, thank you too. Could you explain why is the second version "better yet"? – Andyc Sep 13 '16 at 14:14
  • Because it escapes the `.` character which has special meaning in regular expressions, it matches with any other character. On the other hand, `\.` matches only with a literal `.`. – redneb Sep 13 '16 at 14:15