简体   繁体   English

如何使用Perl从文本文件中提取IP地址?

[英]How to extract IP addresses from a text file using Perl?

How do I extract just the IP addresses from a text file which has an IP address per line? 如何从每行具有IP地址的文本文件中提取IP地址? I would like to extract the IPs and then list the IP addresses in a separate file. 我想提取IP,然后在单独的文件中列出IP地址。 The text file that contains the IPs are in the following format: 包含IP的文本文件采用以下格式:

Host somehost.com (192.168.1.1) is up (0.20s latency). 主机somehost.com(192.168.1.1)启动(延迟0.20秒)。
Host 10.1.0.0 is up (0.21s latency). 主机10.1.0.0启动(延迟0.21秒)。
Host 172.1.0.0 is up (0.21s latency). 主机172.1.0.0启动(延迟0.21秒)。


I'm trying to get the resulting text file to output as follows: 我正在尝试将生成的文本文件输出如下:

192.168.1.1 192.168.1.1
10.1.0.0 10.1.0.0
172.1.0.0 172.1.0.0

What is the best way to do this using Perl? 使用Perl执行此操作的最佳方法是什么?

Note: It doesn't require an regular expression that account for valid IPs...just the IPs in the above format will do. 注意:它不需要考虑有效IP的正则表达式......只需采用上述格式的IP即可。

Thanks! 谢谢!

use Regexp::Common qw/net/;
while (<>) {
  print $1, "\n" if /($RE{net}{IPv4})/;
}

while(<>)
{
  print "$1\n" if /\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/;
}

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

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