This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Deleting several lines from a textfile

Help for those learning Tcl or writing their own scripts.
Post Reply
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Deleting several lines from a textfile

Post by darton »

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?
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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: Select all

 tag when posting logs, code
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

I know how to delete one line. On this forum there is a tutorial for it:

Code: Select all

# 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.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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: Select all

 tag when posting logs, code
Post Reply