Lemon
Lemon

Reputation: 67

Extract IP addresses & CIDR from text using regex in a linux shell

Similar to this, I need to parse a text file for IP addresses and CIDR, so 0.0.0.0/24 or similar. How can this be done, preferably with grep?

Thanks!

Upvotes: 0

Views: 5137

Answers (1)

isedev
isedev

Reputation: 19631

You could use egrep and a regular expression:

egrep '[0-9]{1,3}(?:\.[0-9]{1,3}){0,3}/[0-9]+' /path/to/file

This will match all forms of IP/CIDR:

10/8
100.10/16
192.168.1/24
199.199.199.199/32

Although it will also match incorrect CIDR values, so such:

10/23482347234

Upvotes: 4

Related Questions