当我从 Perl 调用 awk 语句时,为什么它不起作用?
我使用 AWK 的以下命令作为独立命令没有问题, 没有任何错误:
$ awk '$9 != "NTM" && $9 != ""' myfile.txt | less -Sn
但是当我将它们应用于 qsub 的 Perl 脚本中时(即在 linux cluster 中运行 job) 命令,如下所示:
use strict;
use Data::Dumper;
use Carp;
use File::Basename;
my $path = "/mypath/";
my @files = glob($path."*.txt");
foreach my $file ( @files ) {
print "$file\n";
wy $base = basename($file,".fc-gn_".$type);
my $nn = $path.$base.".out";
print "$file $nn\n";
open PIPE, "| qsub" or die $!;
print PIPE <<EOF;
#!/bin/sh
#PBS -N CLEAN
#PBS -l nodes=1:ppn=2
#PBS -r n
awk '$9 != "NTM" && $9 !=""' $file > $nn
EOF
}
它给出了以下错误
awk: cmd. line:1: != "NTM" && !=""
awk: cmd. line:1: ^ syntax error
正确的方法是什么?
I have no problem using the following command of AWK as a stand alone command,
without any error:
$ awk '$9 != "NTM" && $9 != ""' myfile.txt | less -Sn
But when I apply them inside Perl's script for qsub (i.e. running job in linux cluster) command, like this:
use strict;
use Data::Dumper;
use Carp;
use File::Basename;
my $path = "/mypath/";
my @files = glob($path."*.txt");
foreach my $file ( @files ) {
print "$file\n";
wy $base = basename($file,".fc-gn_".$type);
my $nn = $path.$base.".out";
print "$file $nn\n";
open PIPE, "| qsub" or die $!;
print PIPE <<EOF;
#!/bin/sh
#PBS -N CLEAN
#PBS -l nodes=1:ppn=2
#PBS -r n
awk '$9 != "NTM" && $9 !=""' $file > $nn
EOF
}
It gave the following error
awk: cmd. line:1: != "NTM" && !=""
awk: cmd. line:1: ^ syntax error
What's the right way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在所有
"$9"
变量之前放置一个"\"
字符即可修复此问题。 它们由 Perl 本身解析。以下脚本:
输出:
但是:
输出:
但是,由于您希望 Perl 解释
$file
和$nn
,因此实际行应该是:Putting a
"\"
character before all of your"$9"
variables will fix it. They're being parsed by Perl itself.The following script:
outputs:
but:
outputs:
However, since you want
$file
and$nn
to be interpreted by Perl, the actual line should be:您可以使用反斜杠解决方案,或者通过使用以下方式使其更具视觉吸引力:
print <<'EOF'
(并且没有反斜杠),如果您喜欢 Perl,您可能想尝试一下在gearman,这真的很酷。
you can use the backslash solution, or make it more visually appealing by using:
print <<'EOF'
(and no backslash), also if you like Perl, you might want to give a shot at gearman, it's really cool.