Call grep and head from PHP

I am trying to filter the contents of a set of files in a directory and display only the first n lines using the following command:

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

The execution of this command in the terminal takes several seconds. But when I run it with PHP, it takes almost one hour, because the total file size is huge. It seems php is waiting for the gzip command to complete. But if I just run:

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

he gives results immediately. I tried backquotes, exec, system.

here is the php code:

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

How to fix it?

+4
source share
1 answer

Use passthru instead of exec to get all output, not just the last line

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

Source: https://habr.com/ru/post/1487333/


All Articles