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

Hi, I have a line of code that outputs data from an executable to a text file: system "./visacmd.exe /a=USB::0x0699::0x0368::C010731::INSTR /c=MEASU:IMMED:VALUE? >test.txt"; I want to pull a value out of the output to the text file. Can I somehow do that with regex dynamically or is the best way to do it just to post parse the text file?

Replies are listed 'Best First'.
Re: formatting output from system call
by kcott (Archbishop) on Mar 18, 2014 at 00:03 UTC

    G'day mark4444az,

    Perhaps something like this:

    `your_command | tee test.txt` =~ /your_capturing_regex/

    Here's an example:

    $ perl -le '`date | tee test.txt` =~ /^(\w+)/; print $1' Tue
    $ cat test.txt Tue 18 Mar 2014 11:02:34 EST

    Update:

    Your post was a little unclear. If you wanted to capture part of the output and only store the captured part in the ouput file, you could do something like this:

    system "echo @{[`your_command` =~ /your_capturing_regex/]} > test.txt"

    Here's an example of doing it this way:

    $ perl -e 'system "echo @{[`date` =~ /^(\w+)/]} > test.txt"'
    $ cat test.txt Tue

    -- Ken

Re: formatting output from system call
by toolic (Bishop) on Mar 17, 2014 at 18:33 UTC
    You could parse the output of qx:
    qx(./visacmd.exe /a=USB::0x0699::0x0368::C010731::INSTR /c=MEASU:IMMED +:VALUE?);
Re: formatting output from system call
by vinoth.ree (Monsignor) on Mar 17, 2014 at 18:58 UTC
    I want to pull a value out of the output to the text file. 

    You have not shown us the output and what value you want to extraxt from it.

    As toolic told, you can use qx() to execute your comand and capture the output. System() call will return the exit status of your command


    All is well
      The output looks like this: USB::0x0699::0x0368::C010731::INSTR <- MEASU:IMMED:VALUE? USB::0x0699::0x0368::C010731::INSTR -> 9.9E37 I would want to pull the 9.9E37 out of that. The regex part should be easy, but I'm not quite sure how that would work with qx().

        We suggested to use qx() to execute the command and capture the output, into a variable like below, instead of creating file.

        Ex:
        use strict; use warnings; my $output = qx(date); print $output;

        After that you can use your regx on that variable and pull the output value.


        All is well