say the file has:
hello bob
how can i make it search the file for hello and replace bob with another word?
Code: Select all
set word1 "hello"
set word2 "bob"
set word3 "replace"
set file "blah"
file copy -force -- $file "${file}.bak"
set rfp [open "${file}.bak" r]
set wfp [open $file w]
while {![eof $rfp]} {
set s [gets $rfp]
while {[string match "* $word1 $word2 *" $s]} {
set s [string replace $s [string first $word2 $s] [string wordend $s [string first $word $s]] $word3]
}
puts $wfp $s
}
close $wfp
close $rfp
Code: Select all
set text "let's get some sexy goats"
regsub -all -- {(^|W)(sexy) w*} $text {12 sheep} newtext
# now it's "let's get some sexy sheep"