Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2004
    Posts
    1,468

    delete first line of text file.

    Hey,

    Anybody know how to delete the first line of a text file?

    Thanks,
    Jason

  2. #2
    Open the file for reading, read the text file line by line into an array, open the file for writing, loop through the array skipping the first element into the file. PHP.net should answer any questions about the above.
    Dan Grossman - dan @ awio.com
    My Blog | Affiliate Program for Web Hosts

  3. #3
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    Code:
    fyreworks@sandbox fyreworks $ cat test.txt
    a
    b
    c
    fyreworks@sandbox fyreworks $ sed -i '1d' test.txt
    fyreworks@sandbox fyreworks $ cat test.txt
    b
    c
    So, to do the same in PHP:

    PHP Code:
    exec("sed -i '1d' test.txt"); 

  4. #4
    Join Date
    Mar 2004
    Posts
    1,468
    fyrestrtr,
    could you please explain that top code you posted a little bit please?

  5. #5
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    Well, the first bit is just the output from my shell using the sed command. Sed is a stream editor (available on almost all unix and linux system).

    cat is the unix/linux command for concatenation. If you don't give it a second parameter, it concatenates to stdout, which is the screen.

    So, I first created a test.txt file with three lines -- and cat test.txt showed me the file.

    Next I ran sed on it. -i means "edit in place". The part in ' ' is the command that you want sed to perform d is for delete, and 1 is the number of lines.

    So, put it all together and what we are saying is :

    "Delete the first line from test.txt, and update test.txt".

    The php bit is just doing the same. Exec is a function that executes a command and returns the results.

    Hopefully this helps.

  6. #6
    Join Date
    Mar 2004
    Posts
    1,468
    ohhh...ok i get it

    Thanks a lot fyrestrtr!

Posting Permissions

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