I've been googling this for a while now, and have looked at a bunch of stackoverflow things. Issue is, I still don't have an answer and have tried all of the methods I found so far. I need grep to get those 4 lines after the match, and I'm getting unexpected behavior and errors.
Here's my perl script:
#! /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";
And grep's output should be
---------- 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
But the result of running that perl script against a forwarded email is:
macpro:perl test macpro$ ./reject.pl email
doing grep now:
$VAR1 = [
'-iA 4 "forwarded message"',
'reject.txt'
];
grep: Invalid argument
done with grep
Any pointers would be appreciated. As I mentioned, I'm clueless about perl, just now started.
Fix: See @hobbs's reply below. Additionally, for a simpler semantic use for noobs, this other option works too, and you don't have to mess around with arrays:
use Capture::Tiny 'tee';
my $output = tee { system( "some command" ) };
push(@args,"-iA 4 \\"forwarded message\\"");
is wrong. You want to write:
push @args, "-iA", "4", "forwarded message";
so that those appear as three separate arguments to grep.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.