簡體   English   中英

為什么此egrep命令在我的Shell中有效,而在Perl中卻無效?

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

以下命令在Linux中的命令行上有效:

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

但是,當我在Perl腳本中使用它時,它不返回任何內容。 這是Perl代碼:

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

我究竟做錯了什么?

$"是一個perl變量 ,正在反引號內擴展。您需要對美元進行轉義

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

我使用的是qx{}而不是不太明顯的反引號。


另一種方法,使用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