如何在 Perl 中使用 Paste 和 awk?
我有以下使用“粘贴”和 AWK 脚本的代码 珀尔。
use strict;
use Data::Dumper;
use Carp;
use File::Basename;
my @files = glob("result/*-*.txt");
my $tocheck = $ARGV[0] || "M";
foreach my $file ( @files ) {
my $base = basename($file,".txt");
my @res = `paste <\(awk '\$4 == "M" {sum += \$2 }END{print sum}' $file \) <\(awk '\$4 == "M" {sum += \$3 }END{print sum}' $file\)`;
chomp(@res);
print "$base $res[0]\n";
}
为什么会出现这样的错误:
#sh: -c: line 1: syntax error near unexpected token `('
#sh: -c: line 1: `paste <(awk '$4 == "M" {sum += $2 }END{print sum}' result/9547_1-S_aureus.txt ) <(awk '$4 == "M" {sum += $3 }END{print sum}'
#result/9547_1-S_aureus.txt)
正确的方法是什么?
I have the following code that uses 'paste' and AWK script inside
Perl.
use strict;
use Data::Dumper;
use Carp;
use File::Basename;
my @files = glob("result/*-*.txt");
my $tocheck = $ARGV[0] || "M";
foreach my $file ( @files ) {
my $base = basename($file,".txt");
my @res = `paste <\(awk '\$4 == "M" {sum += \$2 }END{print sum}' $file \) <\(awk '\$4 == "M" {sum += \$3 }END{print sum}' $file\)`;
chomp(@res);
print "$base $res[0]\n";
}
Why it gives such error:
#sh: -c: line 1: syntax error near unexpected token `('
#sh: -c: line 1: `paste <(awk '$4 == "M" {sum += $2 }END{print sum}' result/9547_1-S_aureus.txt ) <(awk '$4 == "M" {sum += $3 }END{print sum}'
#result/9547_1-S_aureus.txt)
What's the correct way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不完全确定这是否是对脚本的正确解释,因为那里似乎有很多死/未使用的代码,但肯定不需要生成 Paste 或 awk 来执行此操作:
Not entirely sure if this is a correct interpretation of your script, as there appears to be a lot of dead/unused code there, but there is certainly no need to go spawning paste or awk to do this:
为了解决该错误,Perl 的反引号显式使用 /bin/sh 来运行该命令。 您的 /bin/sh 与 bash 不同,并且不理解“<(进程替换)”语法。
我完全同意从 Perl 调用 awk 是愚蠢的。
To address the error, Perl's backtick explicitly uses /bin/sh to run the command. Your /bin/sh isn't like bash and doesn't understand "<(process substitution)" syntax.
I completely agree that calling awk from Perl is just silly.
下面的命令可以简化吗?
Can the following command be simplified ?