egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Deleting several lines from a textfile

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Fri Jun 16, 2006 5:55 pm    Post subject: Deleting several lines from a textfile Reply with quote

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
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Fri Jun 16, 2006 11:29 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
darton
Op


Joined: 21 Jan 2006
Posts: 155

PostPosted: Sat Jun 17, 2006 6:17 am    Post subject: Reply with quote

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
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Sat Jun 17, 2006 10:13 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber