#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    15
    I want to select a single line from a file. How can I select a line from the file and save it in a variable.Need help Quickly please.
    Thanks in advance..
    Nikunj
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    111
    Rep Power
    14
    can you give some info regarding the line in the file.

    ie. how do you know where the line is in the file and are there any identifiers.

    for example:
    of parsing text files see http://www.firepages.com.au/news.htm
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    15
    Ther is a file called sendmail.cw in my linux sever on deletion of a domain with its name saved in '$dname' variable i want to delete all the directories associated with the domain and modify the various files and delete everything regarding that domain from the files . In this sendmail.cw file various domain name exist one after other. for example:
    yahoo.com
    Xyz.com
    in separate lines I want to select that domain name and delete it from the file.Any help will Be highly appreciated.
    Thanks.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    15
    Can anybody help me in this??????
    This is really urgent . I am stuck up .
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    Ontario, Canada
    Posts
    498
    Rep Power
    18
    You *could* write a short function, call it something like "get_line". Try this code, see if it works:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    function get_line($txtfile,$params) {
    $this = file($txtfile); // put the whole file into an array
    $i = 0;
    $x = 0;
    while($i < sizeof($this)) {
    if ($this[$i] == $params) { $i++; } // skip if it's the line you want deleted
    else { $retval[$x] = $this[$i]; $i++; $x++;} // if not, put into new array and go back through the loop
    }
    system("cat /dev/null >$txtfile"); // clear the file
    $fp = fopen($txtfile,'a+');
    $y = 0;
    while($retval[$y]) {
    fwrite($fp,$retval[$y] . "n"); // write new file line until array is finished
    $y++;
    }
    }
    [/code]

    I know this code is sloppy at best, but at least it's something to work with...
    }


    ------------------
    To alcohol! The cause of, and solution to, all of life's problems. -- Homer Simpson
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    15
    $oldword="word to be deleted";
    //$list is the name of file to be edited.
    function deleteline($list,$oldword)
    {
    $newword="";
    $results = file($list);
    $newword = "";
    while (list(,$path) = each($results))
    {
    $parts = explode(':', $path);
    $path = $parts[0];
    $fp = fopen("$list", 'r');

    if ($fp)
    {
    $data = fread($fp, filesize($list));
    fclose($fp);
    $newdata = str_replace($oldword, $newword, $data);
    $fp = fopen($list, 'w');

    if ($fp)
    {
    fwrite($fp, $newdata);
    fclose($fp);
    }
    }
    }
    return("<br>Occurences of $dname removed from $list");
    }
    I have triedsomething like this and it works perfectly as i wanted but the only problem with it is that instead of the word i am getting a blank line. Can I delete that line completely.
  12. #7
  13. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    thane
    Posts
    50
    Rep Power
    0
    hi
    that was a nice piece of code from both of u.

    i know nikunj's work is done but
    for my similar work i use sed
    i think u can use sed in
    system()


  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    15
    Can I know what this sed means and how can we use this. How can we use this in System.
    Well hope everybody keeps up the good work.
    And replies soon.

Similar Threads

  1. retrieve a line from a file
    By aleens in forum C Programming
    Replies: 10
    Last Post: February 26th, 2004, 05:05 AM
  2. What is the maximum line length for .csv file?
    By bnow in forum PHP Development
    Replies: 0
    Last Post: February 17th, 2004, 07:40 AM
  3. Replies: 1
    Last Post: February 11th, 2004, 07:47 PM
  4. Opening file, finding line, then inserting new line
    By mach5 in forum Perl Programming
    Replies: 2
    Last Post: February 4th, 2004, 11:00 PM
  5. How to read multiple line text file into a variable
    By gatewaycityca in forum Visual Basic Programming
    Replies: 4
    Last Post: January 29th, 2004, 12:26 PM

IMN logo majestic logo threadwatch logo seochat tools logo