Sfisioza
Sfisioza

Reputation: 3930

Read single line from a big text file

I have a 10MB text file.
The length of the lines may vary.

Which is the most efficient way (fast and memory friendly) to read just one specific line from this file? e.g. get_me_the_line($nr, $file_resource)

Upvotes: 2

Views: 1283

Answers (3)

Carl Zulauf
Carl Zulauf

Reputation: 39558

If the file is REALLY large (several GB or more) and your application is running on *nix you may not want to try having PHP process the file and instead use some existing unix tools optimized for this kind of line processing. Once such tool is sed and an example of printing a specific line from a huge file can be found here.

Should be trivial to wrap this in a system_exec() call, or similar to write the function you are looking for.

Upvotes: 1

JYelton
JYelton

Reputation: 36512

I don't know of a way to just jump to the line, if the lines are of varying length. However you can iterate through lines pretty quickly when not using them for anything, and return the one of interest.

function ReadLineNumber($file, $number)
{
    $handle = fopen($file, "r");
    $i = 0;
    while (fgets($handle) && $i < $number - 1)
        $i++;
    return fgets($handle);
}

Edit

I added - 1 to the loop because this reads a line ahead. The $number is therefore a zero-index line reference. Change to - 2 if you would prefer line 1 mean the first line in the file.

Upvotes: 6

As the lines are of varying length you have to look at each character as it might denote the end of the line. Quickest would be loading the file in chunks that are sized like the blocksize of the filesystem and counting the linebreaks until you are on the desired line. Better way would be to have an index file that stores information about the file containing the lines. Using a database could also be a better idea.

Upvotes: 2

Related Questions