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? 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:
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 try开发者_JAVA百科ing to get the resulting text file to output as follows:
192.168.1.1
10.1.0.0 172.1.0.0What is the best way to do this using Perl?
Note: It doesn't require an regular expression that account for valid IPs...just the IPs in the above format will do.
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/;
}
精彩评论