在perl脚本中粘贴和awk错误

sar*_*ara 2 perl awk

我用了

paste Output1.txt Output2.txt | awk '{print $1, $2, $5, $15}' 
Run Code Online (Sandbox Code Playgroud)

使用system(awk_commamd)在perl脚本中.我得到的错误就像

awk: {print , , , }
awk:        ^ syntax error
Run Code Online (Sandbox Code Playgroud)

需要帮助来排序此错误

Mil*_*ler 6

复制pasteawk使用纯perl 的工作:

use strict;
use warnings;
use autodie;

my @fhs = map {open my $fh, '<', $_; $fh} qw(Output1.txt Output2.txt);

while (grep {!eof $_} @fhs) {
    my @lines = map {(scalar <$_>) // ''} @fhs;
    chomp @lines;
    my @fields = split ' ', join "\t", @lines;

    print join("\t", grep defined, @fields[0,1,4,14]), "\n";
}
Run Code Online (Sandbox Code Playgroud)

  • +1,eof应该使用显式文件句柄=>`!eof($ _)`作为`没有参数的eof使用最后读取的文件`. (2认同)