perl -MRegexp::Common="net" -lne "print $_ for /$RE{net}{IPv4}:?\d{0,4}/g" ip_data.txt > out.log
The port part of that RegExp is fishy. Yes, it was already that way in the vbs code. It allows zero to four digits, but valid port numbers have one to five digits (0..65535). Also, the colon between IP address and port number is optional.
Errors:
>echo 127.0.0.1:12345 | perl -MRegexp::Common=net -lne 'print $_ for /
+$RE{net}{IPv4}:?\d{0,4}/g'
127.0.0.1:1234
>echo 127.0.0.1239999 | perl -MRegexp::Common=net -lne 'print $_ for /
+$RE{net}{IPv4}:?\d{0,4}/g'
127.0.0.1239999
A little bit better: Make the entire port number, including the colon, optional.
>echo 127.0.0.1:12345 | perl -MRegexp::Common=net -lne 'print $_ for /
+$RE{net}{IPv4}(?::\d{1,5})?/g'
127.0.0.1:12345
>echo 127.0.0.1239999 | perl -MRegexp::Common=net -lne 'print $_ for /
+$RE{net}{IPv4}(?::\d{1,5})?/g'
127.0.0.123
But that also allows invalid port numbers larger than 65535. It's possible to replace \d{1,5} with a much more complicated expression that allows only positive integers up to 65535, but it is much easier to capture the port value and compare it to 65535 after the RegExp has matched. Depending on the input data, it might be easier to pretend that port numbers can't exceed 9999 and use \d{1,4} instead.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
|