如何在 Perl 中使用 Paste 和 awk?

发布于 2024-07-16 21:24:50 字数 796 浏览 8 评论 0原文

我有以下使用“粘贴”和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉世弥音 2024-07-23 21:24:50

不完全确定这是否是对脚本的正确解释,因为那里似乎有很多死/未使用的代码,但肯定不需要生成 Paste 或 awk 来执行此操作:

#!/usr/bin/perl
use warnings;
use strict;
use File::Basename;

my @files = glob ("result/*-*.txt");

foreach my $file (@files) {
   open (FILE, $file) or die "open $file: $!\n";
   # You seem to be summing the 2nd and 3rd columns if the 4th is "M"
   my ($col1, $col2) = (0, 0);
   while (<FILE>) {
       my @cols = split /\s+/;
       if ($cols[3] eq "M") {
          # Perl uses 0-based arrays, unlike awk
          $col1 += $cols[1];
          $col2 += $cols[2];
       }
   }
   close FILE;
   printf "%s %d\n", basename ($file), $col1;
}

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:

#!/usr/bin/perl
use warnings;
use strict;
use File::Basename;

my @files = glob ("result/*-*.txt");

foreach my $file (@files) {
   open (FILE, $file) or die "open $file: $!\n";
   # You seem to be summing the 2nd and 3rd columns if the 4th is "M"
   my ($col1, $col2) = (0, 0);
   while (<FILE>) {
       my @cols = split /\s+/;
       if ($cols[3] eq "M") {
          # Perl uses 0-based arrays, unlike awk
          $col1 += $cols[1];
          $col2 += $cols[2];
       }
   }
   close FILE;
   printf "%s %d\n", basename ($file), $col1;
}
余生再见 2024-07-23 21:24:50

为了解决该错误,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.

余罪 2024-07-23 21:24:50

下面的命令可以简化吗?

my $inputString = "paste <\(grep \"target:\" $gTestFile | awk '{print \$4,\$5,\$6,\$7,\$8,\$10,\$11,\$12,\$15,\$16,\$17}'\) $preFile";

my @combinedOutput = `$inputString`;

Can the following command be simplified ?

my $inputString = "paste <\(grep \"target:\" $gTestFile | awk '{print \$4,\$5,\$6,\$7,\$8,\$10,\$11,\$12,\$15,\$16,\$17}'\) $preFile";

my @combinedOutput = `$inputString`;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文