Sponsored Content
Full Discussion: tcl problem
Top Forums Shell Programming and Scripting tcl problem Post 302215123 by ramen_noodle on Tuesday 15th of July 2008 03:13:37 PM
Old 07-15-2008
Code:
#!/usr/bin/tclsh
#script to append data to file safely.
set fnametarget [lindex $argv 0]
set fnametemp [expr {[expr [string length [lindex $argv 2]] > 0]  ?  [lindex $argv 2] : "/tmp/tmpfile.[clock format [clock seconds] -format %s]"}]
set pattern [lindex $argv 1]

proc writetoreplacement {lst fn {dbg 0}} {
set i 0
set toohandle {}
                        if {[catch {set toohandle [open $fn "w"]} err]} {puts "Error: In procedure writetoreplacement -- $err." ; return -1}
                        for {set i 0} {$i < [llength $lst]} {incr i} {
                            puts $toohandle [lindex $lst $i]
                            if {$dbg > 0} {puts "Wrote [lindex $lst $i] to descriptor $toohandle"}
                        }
                        close $toohandle
                        return $i
}
                            
                             

#main
if {![string length $fnametarget] || ![string length $pattern]} {puts "Error: Please provide -- 1. filename, 2. regexp to find in file, 3 (Optional) name of temp file." ; exit}
if {[catch [file copy -force $fnametarget $fnametemp] err]} {puts "Error: Unable to copy file $fnametarget to $fnametemp -- $err" ; exit}
if {[catch {set fd [open $fnametarget "r"]} err]} {puts "Error: could not open target file $fnametarget." ; exit}

#file is copied and opened with descriptor structure at $fd. read file by line looking for pattern.
set count 0
set rep {}
set linelist {}
set tmpline ""
 
while {[gets $fd line] > 0} {
       incr count
       lappend linelist $line
       if {[regexp $pattern $line]} {
           puts -nonewline "\nFound pattern $pattern at line # $count. Please enter appending string: "
           gets stdin rep
           if {![string length $rep]} {continue}
           set tmpline [lindex $linelist [expr $count - 1]]    
           append tmpline $rep
           set linelist [lreplace $linelist [expr $count - 1] [expr $count - 1] $tmpline]
           puts "Committed change at $count = [lindex $linelist [expr $count - 1]]" 
       }
}

puts "Committing...."
after 1000
writetoreplacement $linelist $fnametemp
if {[catch [file copy -force $fnametemp $fnametarget] err]} {puts "Error comitting changes: $err"; exit}
file delete -force $fnametemp

You'll need to devise logic to take advantage of the backup file created to be safe.
You could add an additional argument to the script for non-interactive line editing, or have the script read from another data source for per line replacement.
Have fun.

Last edited by ramen_noodle; 07-15-2008 at 04:27 PM.. Reason: oops
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

problem with diff and tcl scripts freaking out unix

running solaris 2.5.1 on a sparc5, with less than 12 users runnign compilers, gui's, really not a heavy load on it however, sometimes, not always, when users run diff, or sdiff or a .tcl script, the computer locks up. One time right before everything froze, I noticed in top, that the sdiff process... (2 Replies)
Discussion started by: kymberm
2 Replies

2. Shell Programming and Scripting

TK/TCL Help

can someone tell my why the puts is not reflecting the variable? CUT --> set fp while {-1 != } { button .a${line} -text "${line} " -width 20 -command { puts $line } pack .a${line} } (0 Replies)
Discussion started by: hpuxrox
0 Replies

3. Shell Programming and Scripting

Help with Tcl...

Hello Tcl Experts, set i 0 while { $i < 10 } { puts "$i" incr i } I am trying to print the value of "i" at the same place. i.e. it should first print "1", then in next iteration print "2" over the location of "1" and so on.... (i.e. in every iteration, delete the previous number and... (2 Replies)
Discussion started by: sumitgarg
2 Replies

4. UNIX for Dummies Questions & Answers

problem with tk_optionmenu in tcl/tk

hi how can i see all and select one option from tk_optionMenu in tcl/tk using keyboard only. Thanks (0 Replies)
Discussion started by: pankajbgarh
0 Replies

5. Programming

Tcl - SOAP Problem

Hi, Im working with client side Tcl implementation on unix box of web services, to login to a tool with web service method written in C# on windows box and it is accessed by its link from the browser on unix box. Sorry that i have hidden the original names for security reasons. Using... (0 Replies)
Discussion started by: SankarV
0 Replies

6. Shell Programming and Scripting

Need your help - tcl

Hello, Can someone explaine me the meaning of this program: #! /usr/bin/tclsh set mctal set a set b set c set d set e while {! line cell]} { } while {! line]} { } while {! line cell]} { } while {! line]} { } (0 Replies)
Discussion started by: jolecanard
0 Replies

7. Shell Programming and Scripting

Writing in a socket trough TCL problem.

Hello everyone: I have a script that writes and read DATA in/from a socket, but i have a great problem while writing. My socket is created in this way: set connection fconfigure $connection -encoding binary -translation binary -blocking 1 And the writer procedure is this one: ... (0 Replies)
Discussion started by: trutoman
0 Replies

8. Shell Programming and Scripting

Problem capturing output in TCL script

I have a TCL script that logs into a switch using expect.I send a command "show port-security address" and it returns a table having a large number of rows.I need to capture this output(the table) and store it in a .txt file. I have done this: match_max 5000 set expect_out(buffer) {} set... (0 Replies)
Discussion started by: plasmalightwave
0 Replies

9. Shell Programming and Scripting

help in tcl...

how can i make a list with the n last files and their details in tcl?.. thanks. (0 Replies)
Discussion started by: eee
0 Replies

10. UNIX for Advanced & Expert Users

Problem with TCL package

Following error encountered while executing characterizationEngine.tcl :confused::confused: $ ./characterizationEngine.tcl Tcl Client is running Ixia Software version: 5.60 Can't find tclx.tcl in the following directories: /usr/lib/tclX8.3 /usr/lib/tclX8.3 /usr/lib/tclX8.3... (0 Replies)
Discussion started by: hiten.r.chauhan
0 Replies
source(n)						       Tcl Built-In Commands							 source(n)

__________________________________________________________________________________________________________________________________________________

NAME
source - Evaluate a file or resource as a Tcl script SYNOPSIS
source fileName source -encoding encodingName fileName | _________________________________________________________________ DESCRIPTION
This command takes the contents of the specified file or resource and passes it to the Tcl interpreter as a text script. The return value from source is the return value of the last command executed in the script. If an error occurs in evaluating the contents of the script then the source command will return that error. If a return command is invoked from within the script then the remainder of the file will be skipped and the source command will return normally with the result from the return command. The end-of-file character for files is "32" (^Z) for all platforms. The source command will read files up to this character. This restriction does not exist for the read or gets commands, allowing for files containing code and data segments (scripted documents). If you require a "^Z" in code for string comparison, you can use "32" or "u001a", which will be safely substituted by the Tcl interpreter into "^Z". The -encoding option is used to specify the encoding of the data stored in fileName. When the -encoding option is omitted, the system | encoding is assumed. EXAMPLE
Run the script in the file foo.tcl and then the script in the file bar.tcl: source foo.tcl source bar.tcl Alternatively: foreach scriptFile {foo.tcl bar.tcl} { source $scriptFile } SEE ALSO
file(n), cd(n), encoding(n), info(n) KEYWORDS
file, script Tcl source(n)
All times are GMT -4. The time now is 07:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy