SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast reynierpm's Avatar
    Join Date
    Dec 2004
    Location
    Cuba
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete a specific line in a TXT file

    How can I delete a specified line in a txt file. For example mi txt file has this content:
    Configuration file
    ~~~~~~~~~~~~~~~~~~
    RAR for Unix reads configuration information from the file .rarrc
    in the user's home directory (stored in HOME environment variable)
    or in /etc directory.

    RAR for Windows reads configuration information from the file rar.ini,
    placed in the same directory as the rar.exe file.

    This file may contain the following string:

    switches=<any RAR switches, separated with a space>
    and I want to delete the file contain this character "~", so how I do this?
    Regards

  2. #2
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Iterate through file and create new file at same time. Only copy lines which don't have a ~ character:
    PHP Code:
    <?php 
     $fname 
    "test.txt";
     
    $lines file($fname);
     foreach(
    $lines as $line) if(!strstr($line"~")) $out .= $line
     
    $f fopen($fname"w"); 
     
    fwrite($f$out); 
     
    fclose($f); 
    ?>
    Please backup your file before using this though

  3. #3
    SitePoint Enthusiast reynierpm's Avatar
    Join Date
    Dec 2004
    Location
    Cuba
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    But

    Is it the case I have a character but if I want to delete the line 4? How I do this?

    Regards and thanks for the other post

  4. #4
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Err, i don't really understand your question, but if you want to delete line 4 if it contains ~, then you'd do something like:
    PHP Code:
    <?php
    $line_no 
    0;
    $fname "test.txt";
    $lines file($fname);
    foreach(
    $lines as $line) {
       
    $line_no++;
       if(!
    strstr($line"~") && $line_no == 4$out .= $line;
    }
    $f fopen($fname"w");
    fwrite($f$out);
    fclose($f);
    ?>


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
  •