简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM