grep:grep_ip_address_accurately
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
grep:grep_ip_address_accurately [2019/11/29 16:06] – removed peter | grep:grep_ip_address_accurately [2020/07/16 08:47] (current) – old revision restored (2017/04/03 16:17) 192.99.13.133 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Grep - Grep IP address accurately ====== | ||
+ | |||
+ | Let's say we have one log file named logfile.log that contains a few lines like below: | ||
+ | |||
+ | <file bash 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, | ||
+ | |||
+ | <code bash> | ||
+ | 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 " | ||
+ | |||
+ | To overcome this problem, we have to use grep like this: | ||
+ | |||
+ | <code bash> | ||
+ | grep " | ||
+ | |||
+ | 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. | ||
+ | |||
+ | <code bash> | ||
+ | grep -w 192.168.1.1 logfile.log | ||
+ | |||
+ | 192.168.1.1 | ||
+ | </ | ||
grep/grep_ip_address_accurately.1575043561.txt.gz · Last modified: 2020/07/15 09:30 (external edit)