frops
frops

Reputation: 2365

How to delete last N rows from a large (big) file

Read the entire file of 500-600 MB I can not, since the data are loaded into memory, for me it's too expensive.

I read the file c file_get_content'om limit lines (conditional on 1000 lines). How can I say after that remove specific lines. Without the use of $ f = file.

Read more: I read a very large file to 1000 lines (the first 1000 lines), in its processes them, and depending on conditions, any line you want to remove, and some leave.

I can write the result in a temporary file, and if the script stops or something else, but there is no rollback.

Upvotes: 0

Views: 437

Answers (1)

kmkaplan
kmkaplan

Reputation: 18960

Php has the ftruncate function.

Edit: give the full solution.

$f = fopen($filename, 'r+');
$linepos = array_fill(0, $N + 1, 0);
$i = 0;
while (! feof($f)) {
    $line = fgets($f);
    if ($line !== FALSE) {
        $j = ($i + 1) % ($N + 1);
        $linepos[$j] = $linepos[$i] + strlen($line);
        $i = $j;
    }
}
ftruncate($f, $linepos[($i + 1) % ($N + 1)]);
fclose($f);

Upvotes: 3

Related Questions