tcl problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting tcl problem
# 1  
Old 07-15-2008
tcl problem

I'm trying to write a script that will do the following:

Open a text file for reading/writing.

Read each line.

When I find a specfic value in a line, append something to the end of that line.

I loop through and read all the lines in myfile.txt looking for dev123_data_log and find a line:

dev123_data_log -rec=system,rw,root=this_server_name

I want to append something to the end of the line.

So, after I'm done, I want to have

dev123_data_log -rec=system,drw,root=server_name,that_server_name


Any help would be appreciated.
# 2  
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
# 3  
Old 07-15-2008
Thank you. It might take me a some time to digest this, but I'll get back to you when I get it figured out.
# 4  
Old 07-25-2008
This worked out very well for me. Thanks again.
# 5  
Old 02-05-2009
TCL Problem

Hello guys, please help me
I want to use TCl to move the lines with line number as odd number to a new file source_odd.txt, and move the lines with character 'a' to another file source_a.txt.
# 6  
Old 02-05-2009
Another TCL function

I also want to create a tcl function to list all the file names in a appointed directory.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question