hegaa has asked for the wisdom of the Perl Monks concerning the following question:

hi I want to transfer this code from VBS to Perl can someone help me?

Set objFS = CreateObject("Scripting.FileSystemObject") Set objRE = New RegExp objRE.Pattern = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,4}" objRE.Global = True objRE.IgnoreCase = False objRE.MultiLine = True strFile = "file.txt" strFileContents = objFS.OpenTextFile(strFile).ReadAll Set colMatches = objRE.Execute(strFileContents) Dim Stuff, myFSO, WriteStuff, dateStamp dateStamp = Date() Set myFSO = CreateObject("Scripting.FileSystemObject") Set WriteStuff = myFSO.OpenTextFile("output.txt", 8, True) For Each objMatch In colMatches WriteStuff.WriteLine(objMatch.Value) Next

Replies are listed 'Best First'.
Re: transfer form vbs to perl
by haukex (Archbishop) on Mar 02, 2017 at 16:46 UTC

    I don't know exactly how regular expressions operate in VBS, but here's my take:

    $ perl -nle 'print for /(?:\d{1,3}\.){3}\d{1,3}:\d{1,4}/g' file.txt >o +utput.txt

    If you want to learn to write your own script, the pages perlintro and perlretut will give you everything you need to know for this task.

    Update: It seems you've now posted your sample input and output data as a root node here. Next time, please put everything into one post, and try writing some code first, since nodes that show more effort usually tend to get better and faster replies. See also How do I post a question effectively?

Re: transfer form vbs to perl
by Discipulus (Canon) on Mar 03, 2017 at 08:52 UTC
    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.
      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". ;-)
Re: transfer form vbs to perl
by sierpinski (Chaplain) on Mar 02, 2017 at 16:32 UTC

    Welcome to the Monastery!

    What have you tried already? Do you have any part of it already converted into perl?

    This really isn't a freelance code design site, but rather an "ask and answer" site. Monks would be glad to help you if you can provide some work you've already tried or something in particular you are having problems with.

Re: transfer form vbs to perl
by Anonymous Monk on Mar 02, 2017 at 20:05 UTC
    while (<DATA>) { print "$_\n" for /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,4})/gi; } __DATA__ 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
    You can change the code to following, save it to file named script.pl and run it like so: perl script.pl < file.txt
    use strict; use warnings; while (<>) { print "$_\n" for /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,4})/gi; }