Thursday, July 17, 2008

Match an IP range using egrep - bash

The input file:

$ cat myhosts.txt mickwe tests1 tests3 tests3 devenv3 dennet5 lic4 tests6

Required: Match IP range from to from the above file.

The solution using egrep is:

$ egrep '172\.22\.21\.([1-9]|(1[0-9]|2[0-2]|3[0-5])) ' myhosts.txt

Output: tests1 tests3 devenv3 dennet5 lic4


Anonymous said...

Thank you. I would use a regular expression not only with egrep.

Unknown said...

egrep '([0-9]{1,3}\.){3}[0-9]{1,3}' /etc/hosts

© Jadu Saikia