An efficient search for IP addresses using Regular Expressions

316 Views Asked by At

I would like to find the most efficient regular expression to find three IP addresses in one search, but I'm not sure if there is a more efficient (faster) syntax that I could use.

I've tried searching for them one address at a time, but I'm curious if there is a faster way.

zgrep -a -i  192\.168\.1\.(10|23|34) *.* >> Results.txt

I'm not getting any errors. I'm really just trying to find out if there is a faster syntax I could be using.

2

There are 2 best solutions below

0
jspcal On

Removing the ignore case -i flag may make it faster. For fixed string matches, such as the 3 possible matches in your example, grep -F or grep -f is also useful.

You could also use sift if you have very large files.

0
Ed Morton On

idk about faster (maybe the removal of -a and -i will make a difference, idk) but this will be more accurate as it'll avoid false matches against longer strings that contain your target IP addresses as substrings:

zgrep -E '(^|[^0-9])192\.168\.1\.(10|23|34)([^0-9]|$)' file

if that's not a concern then this will be faster:

printf '192.168.1.10\n192.168.1.23\n192.168.1.34\n' | zgrep -F -f- file