Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From time to time I want to grep CIDR ranges out of my Apache log files. This is easy for ranges that fall on the natural boundaries (/8, /16 and /24) but not so easy for other ranges such as /17 and /25.


# (easy)
grep " 192\.168\." access_log

# (more thought required)
grep -E " 192\.168\.(12[89]|1[3-9][0-9]|2[0-5][0-9])\." access_log

# (more thought required)
grep -E " 192\.168\.([0-9]|[0-9][0-9]|1[01][0-9]|12[0-7])\." access_log

# (straining my brain)
grep -E " 192\.168\.(1[2-8][0-9]|19[01])\." access_log

These regexes ignore IP addresses that include leading zeros, such as, which isn't a problem in Apache log files but could be in other log files. Printers in particular seem to like the leading zeroes. It's easy enough to add the optional zeroes to the regex but it just makes the whole thing a little bit more difficult. There has to be an easier way.

Is there an easy way to select lines from a file that match any CIDR range?

Fancy regex extensions will be considered as will different tools (such as awk or perl if necessary but I want it to be a one-liner) if they make the job easier. Ideally, what I'd like is something like

grep "[:CIDR]" access_log

A tool that converts a CIDR range into the appropriate regex would also be OK.

$ cidr2regex


$ grep -E "$(cidr2regex" access_log

Bonus points if your answer also covers IPv6.

share|improve this question
While searching around this I found a web-based tool for converting IP ranges to regexes. – Ladadadada Jul 23 '12 at 17:05
And now it has moved here. – Ladadadada Apr 11 '13 at 15:27
up vote 13 down vote accepted

There is, unsurprisingly, a tool for this: grepcidr.

It is not included by default with any system I'm aware of, but you can download it from here, and it is in both the Ubuntu package repository and the FreeBSD ports collection as well.

(Version 2.0 works with IPv6 networks too)

share|improve this answer
If you need IPv6 functionality something horrible could be hacked together using Net::CIDR in perl... – voretaq7 Jul 23 '12 at 17:04

The recently released rgxg command line tool generates regular expressions that match all addresses in a CIDR block:

$ rgxg cidr


$ rgxg cidr 2001:db8:a:b:c:d::/112

For more information, see

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.