| View previous topic :: View next topic |
| Author |
Message |
darton Op
Joined: 21 Jan 2006 Posts: 155
|
Posted: Fri Jun 16, 2006 5:55 pm Post subject: Deleting several lines from a textfile |
|
|
Hello!
I made a script but now I can't get on because I don't know how to delete all lines except for the first. That means I need a code that deletes all lines from the second till the end. Can anybody help me? |
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Fri Jun 16, 2006 11:29 pm Post subject: |
|
|
after so many requests for help, you should have known by now that any manipulation of text files is best done in memory - that is, you read the entire file into a list, manipulate the list, and write it back to disk file _________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
darton Op
Joined: 21 Jan 2006 Posts: 155
|
Posted: Sat Jun 17, 2006 6:17 am Post subject: |
|
|
I know how to delete one line. On this forum there is a tutorial for it:
| Code: | # Use the code from above (1.) to read in all the lines from the file.
# We continue right after: set lines [split $data "\n"]
# We'll delete the first line.
set line_to_delete 0
# If you wanted to delete the last line instead, you would do this:
# set line_to_delete [expr [llength $lines] - 1]
# Now, we remove the line from the list in memory first.
set lines [lreplace $lines $line_to_delete $line_to_delete]
# And finally, we re-write the file with the new data.
set fp [open $fp "w"]
puts $fp [join $lines "\n"]
close $fp |
But here only one line is deleted. What do I have to change that all lines from the second till the end are deleted. |
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Sat Jun 17, 2006 10:13 pm Post subject: |
|
|
so you are somehow reluctant to actually check out [lreplace] docs and see if it's able to delete more than one line? _________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
|