Re: php grep



foodigi wrote:
i am looking for a way to grep through a LARGE text file quickly and
efficiently. i need to pull out just one line of the file. i have found
the most efficient method so far to be:

$handle = popen('grep regex /path/to/file.txt', 'r');
$output = fread($handle, 2096);
pclose($handle);

i have used most other methods of reading text in php, but they have
proved to be very inefficient, and security is a major issue. any help
with this subject will be much appreciated.

If you cant touch the file (chunk it down, transform it), then I seriously doubt you will find anything faster than using grep the way you do.


If you really are only interested in one line then add --max-count=1 as argument to grep. ('grep --max-count=1 regex /path/to/file.txt')
Then it will skip the rest as soon as the first line is found.


Also might be easier to use the exec command like:
  $the_line = exec('grep...', $lines, $rtn_code);

Which as a bonus, will give you the return code, which allways is a good thing to check, if you want to distinguish between an expression not found and an error.

And finally, regarding security, if "regex" is based on some user-input, you might want to do something like:
$arg = escapeshellarg( $user_input );
$cmd = "grep --max-count=1 $arg /path/to/file.txt";



/Bent .



Relevant Pages

  • RE: Internet Explorer History
    ... If so, pull the account ... Subject: Internet Explorer History ... Concerned about Web Application Security? ... Download FREE whitepaper on how a managed service can help ...
    (Pen-Test)
  • Re: Photo of WTC
    ... To Conceal Its Dark Secrets About 911 ... The plane was not flying tin, and the concrete was not 28 feet thick. ... Oh, security, my fucking ass. ... Only a whack job would believe that any organization could pull everything together which would've been necessary to pull off the whole 911 episode and make it look like something else without having it exposed by now. ...
    (alt.sports.football.pro.ne-patriots)
  • Re: Disable Account
    ... but I don't know the exact right as I've never thought about doing ... Pull up the properties on an OU or container and choose security, ...
    (microsoft.public.win2000.active_directory)
  • Re: Group members
    ... > I am working on an internal aspnet application that will pull some security ... > I have a special user Security Group created in the AD. ...
    (microsoft.public.dotnet.framework.aspnet)