简体   繁体   English

为什么此egrep命令在我的Shell中有效,而在Perl中却无效?

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

The following command works on the command line in Linux: 以下命令在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. 但是,当我在Perl脚本中使用它时,它不返回任何内容。 This is the Perl code: 这是Perl代码:

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 $"是一个perl变量 ,正在反引号内扩展。您需要对美元进行转义

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

I'm using qx{} instead of the less-visible backticks. 我使用的是qx{}而不是不太明显的反引号。


Another method, use open and pass each argument as a separate parameter: 另一种方法,使用open并将每个参数作为单独的参数传递:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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