简体   繁体   English

在Linux Shell中使用正则表达式从文本中提取IP地址和CIDR

[英]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. 类似,我需要为IP地址和CIDR解析文本文件,因此为0.0.0.0/24或类似的文件。 How can this be done, preferably with grep? 如何做到这一点,最好是使用grep?

Thanks! 谢谢!

You could use egrep and a regular expression: 您可以使用egrep和一个正则表达式:

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

This will match all forms of IP/CIDR: 这将匹配所有形式的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: 尽管它还会匹配不正确的CIDR值,但是这样:

10/23482347234

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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