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.

Change line in a text file

Help for those learning Tcl or writing their own scripts.
Post Reply
j
joke
Voice
Posts: 13
Joined: Fri Aug 19, 2005 7:20 am
Location: Karlsruhe
Contact:

Change line in a text file

Post by joke »

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.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

This proc written by user might help you:

Code: Select all

# 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?\"" 
   } 
}
Post Reply