0

I'd like to read a large file line by line, perform string replacement and save changes into the file, IOW rewriting 1 line at a time. Is there any simple solution in PHP/ Unix? The easiest way that came on my mind would be to write the lines into a new file and then replace the old one, but it's not elegant.

2 Answers 2

1

I think there're only 2 options

  1. Use memory

Read, replace then store the replaced string into memory, once done, overwrite the source file.

  1. Use tmp file

Read & replace string then write every line immediately to tmp file, once all done, replace original file by tmp file

The #1 will be more effective because IO is expensive, use it if you have vast memory or the processing file is not too big.

The #2 will be a bit slow but be quite stable even on large file.

Of course, you may combine both way by writing replaced string by chunk of lines to file (instead of just by line)

There're the simplest, most elegant ways I can think out.

Sign up to request clarification or add additional context in comments.

1 Comment

That's right for small files RAM, for large ones a temporary file
0

It seems it's not such a bad solution to use the temporary file in most cases.

$f='data.txt';
$fh=fopen($f,'r+');
while (($l=fgets($fh))!==false) file_put_contents('tmp',clean($l),FILE_APPEND);
fclose($f);
unlink($f); 
rename('tmp',$f);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.