简体   繁体   中英

Why does this egrep command work in my shell but not in Perl?

The following command works on the command line in Linux:

egrep -r -i -I -H -A5 "^name#maria.*?#[0-9]{4}#.*?#.*?#.*?$" .

But when I use it inside a Perl script, it doesn't return anything. This is the Perl code:

my @rows = `egrep -r -i -I -H -A5 "^name#maria.*?#[0-9]{4}#.*?#.*?#.*?$" .`;

What am I doing wrong?

$" is a perl variable and is being expanded inside the backticks. You'll need to escape the dollar

my @rows = qx{egrep -r -i -I -H -A5 "^name#maria.*?#[0-9]{4}#.*?#.*?#.*?\$" .};

I'm using qx{} instead of the less-visible backticks.


Another method, use open and pass each argument as a separate parameter:

use autodie qw/open close/;
my @command = ('egrep','-r','-i','-I','-H','-A5','^name#maria.*?#[0-9]{4}#.*?#.*?#.*?$','.');
open my $pipe, '-|', @command;
chomp( my @rows = <$pipe> );
close $pipe;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM