Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用perl系统grep_Perl_Shell_Mod Perl - Fatal编程技术网

使用perl系统grep

使用perl系统grep,perl,shell,mod-perl,Perl,Shell,Mod Perl,使用下面的perl grepregex,它就能正常工作 my @cont = grep {/,\s*511747450\s*,\s*CAN2\s*$/} @fileContents; 我想将它转换为unix系统grep,我用下面的方法使用system命令尝试了相同的正则表达式,但它不起作用 my $cmd="grep ,\s*5117474501\s*,\s*CAN2\s*\$ " . $dirPath . "/" .$fileName; my $exitStatus =syst

使用下面的perl grep
regex
,它就能正常工作

  my  @cont = grep {/,\s*511747450\s*,\s*CAN2\s*$/} @fileContents;
我想将它转换为unix系统
grep
,我用下面的方法使用
system
命令尝试了相同的正则表达式,但它不起作用

  my $cmd="grep ,\s*5117474501\s*,\s*CAN2\s*\$ " . $dirPath . "/" .$fileName;
  my $exitStatus =system($cmd);

在某些版本中,grep不能在bash中使用
\s

尝试
[:space:
而不是
\s


grep的行为因版本而异。

\
*
$
是shell特有的。还有更多的逃跑

use String::ShellQuote qw( shell_quote );

my $pat = ',\\s*5117474501\\s*,\\s*CAN2\\s*$';

my $cmd = shell_quote('grep', '--', $pat, "$dirPath/$fileName");
my $exitStatus = system($cmd);
或者,您可以使用
system
的multi-arg形式简单地避开shell

my $pat = ',\\s*5117474501\\s*,\\s*CAN2\\s*$';

my @cmd = ('grep', '--', $pat, "$dirPath/$fileName");
my $exitStatus = system({ $cmd[0] } @cmd);

非常感谢你提供的信息。System命令是接受一个参数还是两个参数System({$cmd[0]}@cmd)?你能解释一下辩论中通过了什么吗。它是数组还是数组的第一个元素?三是三种形式
system$CMD
system$PROG,@ARGS
system{$PROG}$PROG,@ARGS
谢谢。该示例是否遵循系统{$PROG}$PROG、@ARGS?系统({$cmd[0]}@cmd)无法理解参数中传递的内容。它是数组的第一个元素还是数组本身,因为我在参数中找不到逗号。@Arav,是的。两者都不会传递结果标量列表。