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?
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With