| View previous topic :: View next topic |
| Author |
Message |
joke Voice
Joined: 19 Aug 2005 Posts: 13 Location: Karlsruhe
|
Posted: Thu Nov 09, 2006 3:34 am Post subject: Change line in a text file |
|
|
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 |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Thu Nov 09, 2006 6:12 am Post subject: |
|
|
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 |
|
 |
|