Calling "grep" and "head" from PHP


Calling "grep" and "head" from PHP

我正在尝试过滤目录中文件集的内容,并使用这样的命令只输出前n行:

gzip -dc $(find %pathtofolder%) | grep 27990 | head -n 50

在终端中运行此命令需要几秒钟才能完成。但是当我从PHP中运行它时,它几乎需要一个小时,因为文件的总大小是巨大的。php似乎要等到gzip命令完成。但如果我简单地运行:

gzip -dc $(find /opt/data/bi/ets/20130616) | head -n 50

它立即给出结果。我试了反引号,exec, system.

下面是PHP代码:
$cmd = 'gzip -dc $(find '.$path.' | grep -E "'.$regexp.'") | grep -E "'.$this->_buildRegExp().'" | head -n '.$r['limit'];
$res = `$cmd`;

如何解决这个问题?

使用passthru而不是exec来获取所有输出,而不仅仅是最后一个字符串

passthru($cmd, $output);
echo $output;