1

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
crosser
  • 11
  • 1
  • How do you include a space in the regex ? –  May 24 '19 at 21:10
  • When you append to the file and use one ip regex at a time it will cause the output file to look different probably. Is that a problem ? –  May 24 '19 at 21:13
  • That command won't work. You should get rid of `-i` since caseless is meaningless for digits and dots and hopefully you don't really have binary files inside your zip files so the `-a` isn't necessary either, but then you need to add `-E` for `|` to work. It'll also match IP addrs like `192.168.1.101` and other strings that contains sections that match that regexp due to no boundaries. Post some sample input (i.e. output of zcat) and expected output if you'd like help. See [ask] if that's not clear. – Ed Morton May 25 '19 at 13:19

2 Answers2

0

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.

jspcal
  • 50,847
  • 7
  • 72
  • 76
0

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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185