简体   繁体   中英

Perl send STDIN and filter STDOUT from external program

I'm writing a Perl wrapper for another application.

I need to pipe STDIN and some STDOUT.

Perl code

#!/usr/bin/perl -w

use strict;

use IPC::Run3 qw(run3);

my $stdout;

local $| = 1;

run3 ['node','gekko',"-b","-c","BNB-XLM-Doktor_v1-5-144-config.js"],  undef, $stdout;

Output

2018-03-18 13:50:10 (DEBUG):    Available 142534
2018-03-18 13:50:10 (DEBUG):    Optimal 144240
2018-03-18 13:50:10 (INFO): The database has 1707 candles missing, Figuring out which ones...
2018-03-18 13:50:11 (INFO): Gekko detected multiple dateranges in the locally stored history. Please pick the daterange you are interested in testing:
2018-03-18 13:50:11 (INFO):          OPTION 1:
2018-03-18 13:50:11 (INFO):      from: 2017-12-08 07:04:00
2018-03-18 13:50:11 (INFO):      to: 2018-03-14 19:04:00
2018-03-18 13:50:11 (INFO):          OPTION 2:
2018-03-18 13:50:11 (INFO):      from: 2018-03-16 00:04:00
2018-03-18 13:50:11 (INFO):      to: 2018-03-18 10:04:00
prompt: option: 

I want to achieve this STDOUT:

OPTION 1:
from: 2017-12-08 07:04:00
to: 2018-03-14 19:04:00
OPTION 2:
from: 2018-03-16 00:04:00
to: 2018-03-18 10:04:00
prompt: option: 

So STDOUT must be filtered and I haven't any idea how do this.

I tried with $stdout =~ s/....//g but it's not working.

Remember that it must send STDIN from parent to child too after filtering STDOUT

You can specify a sub instead of a file handle for stdout, it will get each line of the output as an argument:

#!/usr/bin/perl
use warnings;
use strict;

use IPC::Run3 qw(run3);

my $stdout;
local $| = 1;
run3 ['command'], undef, sub {
    $_ = shift;
    return unless /(?:OPTION \d+|from|to|option):\s+/;

    s/.*INFO\):\s+//;
    print
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM