#!/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
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)
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)
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)
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)
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)
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)
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)
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
LEARN ABOUT CENTOS
tclsh
tclsh(1) Tcl Applications tclsh(1)__________________________________________________________________________________________________________________________________________________NAME
tclsh - Simple shell containing Tcl interpreter
SYNOPSIS
tclsh ?-encoding name? ?fileName arg arg ...?
_________________________________________________________________DESCRIPTION
Tclsh is a shell-like application that reads Tcl commands from its standard input or from a file and evaluates them. If invoked with no
arguments then it runs interactively, reading Tcl commands from standard input and printing command results and error messages to standard
output. It runs until the exit command is invoked or until it reaches end-of-file on its standard input. If there exists a file .tclshrc
(or tclshrc.tcl on the Windows platforms) in the home directory of the user, interactive tclsh evaluates the file as a Tcl script just
before reading the first command from standard input.
SCRIPT FILES
If tclsh is invoked with arguments then the first few arguments specify the name of a script file, and, optionally, the encoding of the |
text data stored in that script file. Any additional arguments are made available to the script as variables (see below). Instead of
reading commands from standard input tclsh will read Tcl commands from the named file; tclsh will exit when it reaches the end of the
file. The end of the file may be marked either by the physical end of the medium, or by the character, "