如何使用 Perl 从文本文件中提取 IP 地址? [英] How to extract IP addresses from a text file using Perl?

查看:50
本文介绍了如何使用 Perl 从文本文件中提取 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从每行有一个 IP 地址的文本文件中只提取 IP 地址?我想提取 IP,然后在单独的文件中列出 IP 地址.包含 IP 的文本文件采用以下格式:

How do I extract just the IP addresses from a text file which has an IP address per line? I would like to extract the IPs and then list the IP addresses in a separate file. The text file that contains the IPs are in the following format:

主机 somehost.com (192.168.1.1) 已启动(0.20 秒延迟).
主机 10.1.0.0 已启动(0.21 秒延迟).
主机 172.1.0.0 已启动(0.21 秒延迟).

Host somehost.com (192.168.1.1) is up (0.20s latency).
Host 10.1.0.0 is up (0.21s latency).
Host 172.1.0.0 is up (0.21s latency).


我正在尝试将生成的文本文件输出如下:

I'm trying to get the resulting text file to output as follows:

192.168.1.1
10.1.0.0
172.1.0.0

192.168.1.1
10.1.0.0
172.1.0.0

使用 Perl 执行此操作的最佳方法是什么?

What is the best way to do this using Perl?

注意:它不需要考虑有效 IP 的正则表达式......只要采用上述格式的 IP 即可.

Note: It doesn't require an regular expression that account for valid IPs...just the IPs in the above format will do.

谢谢!

推荐答案

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

这篇关于如何使用 Perl 从文本文件中提取 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆