0

我已经在谷歌上搜索了一段时间,并且查看了一堆 stackoverflow 的东西。问题是,我仍然没有答案,并且已经尝试了到目前为止我发现的所有方法。我需要 grep 才能在比赛结束后获得这 4 行,但我遇到了意外的行为和错误。

这是我的 perl 脚本:

#! /usr/bin/perl
use strict;
use warnings;
use List::Util 'first';  use Data::Dumper;
use IPC::Run 'run';
my $look;
my $file = 'reject.txt';
my @result;
my @args;

#my $from = first { /From:/ } @arr;
#my $to = first { /To: / } @arr;
#my $subject = first { /Subject:/ } @arr;

open my $fh, '>', $file or die $!;
while (<>) {
#print $fh $_;
$look = $_;
print $fh $look;
}
close $fh;
print 'doing grep now:'. "\n";

push(@args,"-iA 4 \"forwarded message\"");
push(@args,$file);

print Dumper(\@args) . "\n";

run [ "grep", @args ], ">" , \my $output;

print $output . "\n";

print 'done with grep' . "\n";

而grep的输出应该是

---------- Forwarded message ----------
From: <email@address.com>
Date: Fri, Oct 4, 2013 at 9:35 AM
Subject: New Voicemail Message from (407) 221-0727
To: recipient@here.us

但是针对转发的电子邮件运行该 perl 脚本的结果是:

macpro:perl test macpro$ ./reject.pl email
doing grep now:
$VAR1 = [
          '-iA 4 "forwarded message"',
          'reject.txt'
        ];

grep: Invalid argument

done with grep

任何指针将不胜感激。正如我所提到的,我对 perl 一无所知,才刚刚开始。

修复:见下面@hobbs 的回复。此外,对于新手来说,为了更简单的语义使用,这个其他选项也可以,你不必乱用数组:

use Capture::Tiny 'tee';
my $output = tee { system( "some command" ) };
4

1 回答 1

1

push(@args,"-iA 4 \"forwarded message\"");是错的。你想写:

push @args, "-iA", "4", "forwarded message";

这样它们就显示为 grep 的三个单独参数。

于 2013-10-12T03:47:06.177 回答