Linux 如何在Perl中使用paste和awk?
我有以下使用“粘贴”和AWK脚本的代码 PerlLinux 如何在Perl中使用paste和awk?,linux,perl,unix,awk,paste,Linux,Perl,Unix,Awk,Paste,我有以下使用“粘贴”和AWK脚本的代码 Perl 使用严格; 使用数据::转储程序; 使用鲤鱼; 使用File::Basename; my@files=glob(“result/*-*.txt”); my$tocheck=$ARGV[0]| |“M”; foreach my$文件(@files){ my$base=basename($file,“.txt”); my@res=`paste不完全确定这是否是对脚本的正确解释,因为那里似乎有很多死代码/未使用的代码,但肯定不需要使用生成paste或a
使用严格;
使用数据::转储程序;
使用鲤鱼;
使用File::Basename;
my@files=glob(“result/*-*.txt”);
my$tocheck=$ARGV[0]| |“M”;
foreach my$文件(@files){
my$base=basename($file,“.txt”);
my@res=`paste不完全确定这是否是对脚本的正确解释,因为那里似乎有很多死代码/未使用的代码,但肯定不需要使用生成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;
}
!/usr/bin/perl
使用警告;
严格使用;
使用File::Basename;
my@files=glob(“result/*-*.txt”);
foreach my$文件(@files){
打开(文件,$FILE)或死亡“打开$FILE:$!\n”;
#如果第四列是“M”,那么第二列和第三列似乎是求和
我的($col1,$col2)=(0,0);
而(){
my@cols=split/\s+/;
如果($cols[3]相等于“M”){
#Perl使用基于0的数组,与awk不同
$col1+=$cols[1];
$col2+=$cols[2];
}
}
关闭文件;
printf“%s%d\n”,basename($file),$col1;
}
为了解决这个错误,Perl的backtick显式地使用/bin/sh来运行命令。您的/bin/sh与bash不同,不理解“下面的命令可以简化吗
my $inputString = "paste <\(grep \"target:\" $gTestFile | awk '{print \$4,\$5,\$6,\$7,\$8,\$10,\$11,\$12,\$15,\$16,\$17}'\) $preFile";
my @combinedOutput = `$inputString`;
my$inputString=“paste perl 1.0被认为是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;
}
my $inputString = "paste <\(grep \"target:\" $gTestFile | awk '{print \$4,\$5,\$6,\$7,\$8,\$10,\$11,\$12,\$15,\$16,\$17}'\) $preFile";
my @combinedOutput = `$inputString`;