简体   繁体   English

Perl从外部程序发送STDIN和过滤STDOUT

[英]Perl send STDIN and filter STDOUT from external program

I'm writing a Perl wrapper for another application. 我正在为另一个应用程序编写Perl包装器。

I need to pipe STDIN and some STDOUT. 我需要管道STDIN和一些STDOUT。

Perl code Perl代码

#!/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: 我要实现此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. 因此,必须对STDOUT进行过滤,而且我不知道该怎么做。

I tried with $stdout =~ s/....//g but it's not working. 我用$stdout =~ s/....//g尝试了,但是没有用。

Remember that it must send STDIN from parent to child too after filtering STDOUT 请记住,在过滤STDOUT之后,它也必须从父级向子级发送STDIN

You can specify a sub instead of a file handle for stdout, it will get each line of the output as an argument: 您可以为stdout指定一个sub而不是一个文件句柄,它将输出的每一行作为参数:

#!/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
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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