Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change text in a file

Status
Not open for further replies.

dfrankel

Programmer
Apr 23, 2002
19
US
I have a file with varibales and their values in it. I would like to update those values directly to the text file. How do I do that?
 
There are several variations of the same way of doing that, depending on how many variables you're talking about. Basically, you have to re-write the file every time. I use a configuration file to set variable defaults that looks like:

set var1 123
set var2 234
...

Then I initialize by "source"ing that file. Then I save that configuration by opening that file for writing (overwriting anything that's there) and putting all the variables I want to keep with:

puts $fid "set var1 $var1"
puts $fid "set var2 $var2"
... Bob Rashkin
rrashkin@csc.com
 
Thanks Bob,
I figured it out, I actually tried doing it a different way. I didn't want to overwrite the file, because I was affraid if it broke in the middle I would lose the whole file.
I did a [gets $channelId line] and split the $line by the = sign. Then I looked at the first variable and if, it was the variable I was looking to update I set a string to the new value and appended it to a list. If it was not the variable I was looking for I just appended the line to the list. In the end I had a list that contained the whole file with the new values. I did a foreach through the list and wrote each line back to a new text file. When I completed that, I executed a shell script to copy the new file over to the old file.

-Daniel
 
I came into this a bit late, but it sounds as though you figured out how to accomplish the task. The only other improvement I have to suggest is that instead of executing a shell script to copy the new file over the old, you can use Tcl's built-in file rename command with the -force option (to force it to overwrite an existing file). In fact, Tcl has several built-in file commands for doing file manipulation of that nature. Using these commands is more efficient than calling out to external programs, and they're cross-platform. Check out the file command documentation for more information.

As an aside, let me mention another strategy for persistent storage. Using a text file as you are now is a nice, simple solution that works in many instances. It doesn't require any extensions; you can easily view it, edit it, and print it; and if the information is going to be used by another application, you might be forced to use that format. But it is inefficient for large amounts of data, especially if you need to modify only a few values out of hundreds or even thousands.

In situations where your primary goal is persistent storage, rather than data interchange, using a database like MetaKit is an excellent alternative. MetaKit is a freely available, cross-platform, open source (MIT-style licensed) database library that you can include in your own programs. It includes bindings for several languages, including Tcl. MetaKit is fast, very light-weight, reliable, and easy to use. It isn't a full-blown relational database, but has more power than a simple flat-file database.

You can get more information about MetaKit from the Tcl'ers Wiki ( on the page The homepage for MetaKit is And an excellent MetaKit tutorial is available from - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thanks Ken,
in the end, we decided not to use a shell script, instead we just used the [exec cp $source $destination] command.

Is there a difference between using TCL file rename or exec cp command?

Daniel Frankel
UBS PaineWebber
 
Most likely, you won't notice a difference between file rename (or file copy) and exec cp in your case. But it's a good idea to know the difference, so you know the tradeoffs.

Tcl's file commands are built into the language, and directly execute the appropriate OS system calls. On the other hand, exec starts a separate process to execute whatever external commands you tell it to run. So when you do an exec cp, the exec command starts a new process (on Unix, the standard fork/exec sequence) and then executes the program specified in that separate process.

For file and directory manipulation like this, I always recommend using Tcl's built-in commands. First, because they're platform-independent. file copy would continue to work even if you ran your script on Windows or Macintosh; but you'd need to re-write your exec command to get it to work properly under Windows (either as "[tt]exec $env(ComSpec) /c copy ...[/tt]" or "[tt][ignore]eval exec [auto_execok copy] ...[/ignore][/tt]"). The second advantage is performance, as file copy doesn't need to create a separate process just to copy a file. If your script did a lot of file manipulation, you'd notice a significant performance benefit in favor of the file commands. - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top