SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    removing lines from a txt file

    Hello

    I have a data.txt file containing data like this

    ...
    house # john
    car # smith
    boat # john
    ...

    I need to remove from file data.txt , all rows having "# john" . I can load the file in array because it's a very big file and I receive a memory error.
    Is there any way to remove all rows having "# john" in the file , without loading the file in array ?

    Thank you
    Graziano
    trcris ::> http://www.trcris.com

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,237
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    You will want to use fgets, write lines you want to keep to a new file, something like the below

    PHP Code:
    $oldfile fopen('mylargefile.txt''r');
    $newfile fopen('mynewlargefile.txt''w');

    while ((
    $currentLine fgets($oldfile)) !== false)
    {
      if (
    strpos($currentLine'# john') === false// line did not contain # john
        
    fwrite($newfile$currentLine);
    }

    fclose($oldfile);
    fclose($newfile); 

  3. #3
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excellent way , thank you.
    trcris ::> http://www.trcris.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •