Grep ip range

I want to find all files under /var directory that refer to any ip from 10.1.1.1 to 10.1.1.255 using grep. How can this be done.

Please help

find /var -type f -print | xargs egrep "10\.1\.1\.([1-9]|[0-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])$"

grep "10.1.1" it will list all occuraneces of "10.1.1"

@aingal: grep "10.1.1" would also match '10x1y1' from whatever file you provide as input.

grep -H -r -E "10\.1\.1\.([1-9]|[0-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])$" /var

Is this ok? What if i remove the $ sign from the end?

Yup, that too is good. You may shorten it as egrep -Hr If you remove $ at the end, it would also match 10.1.1.300, as the first pattern in round parenthesis [1-9] would match 3 and so would show that entry too. You need to specify $ to mean it should end with one of the patterns from round parenthesis.

1 Like