Using grepcidr to find IP addresses in text files


For the longest time I used regular expressions or basic grep patterns to look for IP addresses within text files. These methods make it very difficult to find specific IP addresses in a range if it does not fall on an easy to match pattern.

Use grepcidr to eliminate RegEx and pattern matching. This command will match any IP address in the typical CIDR boundary.

Given a list of IP addresses:

192.168.37.1
192.168.37.2
192.168.37.6
192.168.37.7
192.168.37.10
192.168.37.14
192.168.37.16
192.168.37.17
192.168.37.18
192.168.37.20
192.168.37.22
192.168.37.23
192.168.37.28
192.168.37.30
192.168.37.32
192.168.37.34
192.168.37.36
192.168.37.42
192.168.37.43
192.168.37.48
192.168.37.50
192.168.37.51
192.168.37.52
192.168.37.55
192.168.37.59
192.168.37.61
192.168.37.64
192.168.37.68
192.168.37.72
192.168.37.74
192.168.37.75
192.168.37.77
192.168.37.84
192.168.37.85
192.168.37.88
192.168.37.89
192.168.37.94
192.168.37.95
192.168.37.96
192.168.37.97
192.168.37.98
192.168.37.99
192.168.37.103
192.168.37.106
192.168.37.110
192.168.37.111
192.168.37.114
192.168.37.118
192.168.37.120
192.168.37.121
192.168.37.135
192.168.37.136
192.168.37.137
192.168.37.140
192.168.37.141
192.168.37.157
192.168.37.158
192.168.37.162
192.168.37.165
192.168.37.169
192.168.37.171
192.168.37.174
192.168.37.177
192.168.37.179
192.168.37.180
192.168.37.183
192.168.37.184
192.168.37.185
192.168.37.186
192.168.37.187

Example searches:

jemurray@mbp-2019:~ $ grepcidr 192.168.37.0/30 demo-ips.txt
192.168.37.1
192.168.37.2
jemurray@mbp-2019:~ $ grepcidr 192.168.37.0/29 demo-ips.txt
192.168.37.1
192.168.37.2
192.168.37.6
192.168.37.7
jemurray@mbp-2019:~ $ grepcidr 192.168.37.128/28 demo-ips.txt
192.168.37.135
192.168.37.136
192.168.37.137
192.168.37.140
192.168.37.141