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 

Change line in a text file

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


Joined: 19 Aug 2005
Posts: 13
Location: Karlsruhe

PostPosted: Thu Nov 09, 2006 3:34 am    Post subject: Change line in a text file Reply with quote

Hi,

I've got a text file with many (let's say 1000) lines. The bot should find a certain line (by string match) and replace it with a new one.

I tried to set up a list (each line in the text file represents a list item) and tried to let the bot go through (and replayce the item). So far it worked but I need to change more than 1 line - in fact something about 50 lines. This seems to be a problem (bot crashes), perhaps because my way with this huge list needs too many ressources.

Anyone knows how to make this operation quicker and easier for the bot and can post the code for it?

Thanks a lot.
Back to top
View user's profile Send private message Visit poster's website
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Thu Nov 09, 2006 6:12 am    Post subject: Reply with quote

This proc written by user might help you:
Code:
# Replace one or more lines
# Usage: replaceLines <file> <first line> <last line> [replacement data (optional)]
# Accepts the same values for indexes as 'lreplace' (check your manual)
proc replaceLines {args} {
   if {[llength $args]>=3} {
      foreach {file start end} $args break
      set cmd [list lreplace [split [read [set f [open $file r]]] \n] $start $end]
      close $f
      if {[llength $args]>3} {lappend cmd [lindex $args 3]}
      puts -nonewline [set f [open $file w]] [join [eval $cmd] \n]
      close $f
   } {
      error "wrong # args: should be \"[lindex [info level 0] 0] file start end ?replacement?\""
   }
}

_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
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