Results 1 to 6 of 6
Thread: delete first line of text file.
-
07-13-2004, 10:07 PM #1Web Hosting Master
- 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
-
07-14-2004, 03:49 AM #2Web Hosting Master
- Join Date
- Oct 2003
- Posts
- 1,479
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.
-
07-14-2004, 04:14 AM #3Web Hosting Master
- 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
PHP Code:exec("sed -i '1d' test.txt");
-
07-14-2004, 10:26 PM #4Web Hosting Master
- Join Date
- Mar 2004
- Posts
- 1,468
fyrestrtr,
could you please explain that top code you posted a little bit please?
-
07-15-2004, 02:56 AM #5Web Hosting Master
- 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.
-
07-15-2004, 02:06 PM #6Web Hosting Master
- Join Date
- Mar 2004
- Posts
- 1,468
ohhh...ok i get it
Thanks a lot fyrestrtr!