User Tools

Site Tools


grep:grep_ip_address_accurately

Grep - Grep IP address accurately

Let's say we have one log file named logfile.log that contains a few lines like below:

logfile.log
192.168.1.1
192.168.1.10
192.168.1.11
192.168.1.111

When we want to search for 192.168.1.1, we usually will use:

grep 192.168.1.1 logfile.log
 
192.168.1.1
192.168.1.10
192.168.1.11
192.168.1.111

But unfortunately the result is not as what we expected(I assume we expect only 192.168.1.1 will come out) because grep will show to us all results “containing” the pattern given by us.

To overcome this problem, we have to use grep like this:

grep "192.168.1.1\>" logfile.log
 
192.168.1.1

Do not forget to put the double quotes, if not the command will not show any result.

An alternative way to achieve the above result is by using -w flag of grep.

grep -w 192.168.1.1 logfile.log
 
192.168.1.1
grep/grep_ip_address_accurately.txt · Last modified: 2020/07/16 08:47 by 192.99.13.133

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki