WTing Chong
WTing Chong

Reputation: 11

How to insert "#" comment off to the specific keyword by using Tcl?

Is that any way in TCL to insert the (#) comment off by searching cfg keyword?

-->Before

abcd abcd abcd
cfgabc abcd abcd
cfgcdd abc abc
abcd abcd abcd
cfgabc abcd abcd

-->After run the tcl

abcd abcd abcd
#cfgabc abcd abcd
#cfgcdd abc abc
abcd abcd abcd
#cfgabc abcd abcd

Upvotes: 1

Views: 116

Answers (2)

user2141046
user2141046

Reputation: 912

you can use regsub:
regsub -all {cfg} $str {#cfg}

EDIT
For finding cfg anywhere in the string and commenting this line (as requested in the comments):
regsub {(.*)cfg(.*)} $str {#\1cfg\2}
e.g.

set str "bla lkjhcfglkj"
bla lkjhcfglkj
regsub {(.*)cfg(.*)} $str {#\1cfg\2}
#bla lkjhcfglkj

This is much riskier and requires more attention and caution.

Upvotes: 3

Donal Fellows
Donal Fellows

Reputation: 137687

If you're looking for the three letters cfg at the beginning of a line, that's pretty easy to do with regsub. In particular, the -line option helps a lot with this.

regsub -all -line {^cfg} $str "#cfg"

Note that caution should be taken with this sort of thing: it's extremely easy to either over- or under-match lines. Check the results by hand! (Hopefully it'll be easier than doing lots of edits by hand.)

Upvotes: 2

Related Questions