in reply to transfer form vbs to perl

Hello hegaa

thanks to Perl that saved me from vbs!!

Perl has useful command line switches and many useful modules: Regexp::Common is very useful in you case and prevent 333.444.555.666 to be matched.

#cat ip_data.txt some stuff 10.0.0.1:80 stuff 127.0.0.1:22 more stuffs 10.2.2.8:8080 nothing here 3.4.5.6:1 100.200.100.200:3000 1.2.3.4 perl -MRegexp::Common="net" -lne "print $_ for /$RE{net}{IPv4}:?\d{0,4 +}/g" ip_data.txt > out.log #cat out.log 10.0.0.1:80 127.0.0.1:22 10.2.2.8:8080 3.4.5.6:1 100.200.100.200:3000 1.2.3.4

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: transfer form vbs to perl
by afoken (Chancellor) on Mar 03, 2017 at 10:23 UTC
    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". ;-)