file - TCL, replacing strings in a textfile -
lets open file, parsed lines. use loop:
foreach line $lines {}
e.g., if file contained following string:
xydata, name1
i want put acc_
after xydata
acc_name1
, if file contains more 1 strings xydata
, put vel_
, dsp_
, prs_
, on
using textutil::split
package tcllib, , ability of foreach
iterate on multiple lists simultaneously
package require textutil::split set line {xydata, foo, bar, baz, qux} set prefixes {acc_ vel_ dsp_ prs_} set fields [textutil::split::splitx $line {, }] set new [list] if {[lindex $fields 0] eq "xydata"} { lappend new [lindex $fields 0] foreach prefix $prefixes field [lrange $fields 1 end] { lappend new $prefix$field } } puts [join $new ", "]
xydata, acc_foo, vel_bar, dsp_baz, prs_qux
alternately, use single regsub call generates code
set code [regsub -all {(, )([^,]+)} $line {\1[lindex $prefixes [incr counter]]\2}] set counter -1 puts [subst $code]
Comments
Post a Comment