I am new to perl and want to read a file in which IP are stored , i want to grep these IP and store in array , i am able to write the regex for this but out of three ips it always stores last ip of file in array , please help someone my code and file is like below.
my input file have follwing contents
Hi praveen how are10.23.54.86
10.87.98.65
10.98.76.54
my script o/p is
/pearl$ ./par.pl sdp.txt
10.98.76.54
and code is
#!/usr/bin/perl
open (INPUT,$ARGV[0]) || die "Can not open file:$!";
$i=0;
while(<INPUT>)
{
$line=$_;
($arr,$arr1,$arr2)=($line=~m/\d+[\.]\d+[\.]\d+[\.]\d+/g);
}
close(INPUT);
print $arr,"\n";
I tried with array and as well as variable but same results its only stores last value.
use strictin longer scripts in which case you would have to do that. However perl supports one liners were it's nice to have auto initialization by default.strictandwarningsshould be enabled.