0

Hello!

I have tried using this line of code:

$output = array();
exec('grep "some_keyword" some_file.txt',$output);
echo "<pre>";print_r($output);echo "</pre>";

Upon looking for a keyword that I was sure was in the file, the output was:

Array
(
    [0] =>     
    [1] =>   
)

Otherwise if the keyword was not in the file, it was:

Array
(
)

Any suggestions on how do I fix this and why this is so?

Regards.

John

4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by jrhitokiri
0

2 things:
You may have to give the full path for grep which you can find by running
which grep
from a shell prompt and second what happens when you run the command from the command line?

0

2 things:
You may have to give the full path for grep which you can find by running
which grep
from a shell prompt and second what happens when you run the command from the command line?

full path didn't help

pasting it into command line, it functions normally and outputs the 2 lines (as mentioned in my orig post)

Any other suggestions?

0

Are you sure that it can find the txt file (did you try a full path for that too) ? PHP's current path can be different than the one on your console.

0

Are you sure that it can find the txt file (did you try a full path for that too) ? PHP's current path can be different than the one on your console.

Yep, fill path for both the grep command and the text file.

0

Also have a look at the preg functions of PHP which might be much easier to handle than the external grep.

The thing is, I'm searching a 1GB file and I fear that the preg is significantly slower than the grep external function. :<

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.