how to save config file with date&time in a folder - tcl

I want to automatically back-up the start-up config for my routers, but it will overwrite the previous one. How can I save naming date&time to the back-up file to avoid overwriting.
Please help!

Command clock will help you with time-related stuff. As everything is a string, you can just insert the formatted date to the filename to be opened. w+ means opening file for writing and creating it if it didn't exist.
#!/usr/bin/tclsh
set f [open "[clock format [clock seconds] -format "%Y%m%d-%H%M%S"].txt" w+]
puts $f "foo"
close $f

Related

how to copy a row in a .csv file into a column in another .csv file in tcl ?

I wish to copy a specific row in a .csv file to a specific column in another .csv file using tcl.
What i've tried is to copy the row i wanted into a new .csv file and then copy this row manually into my .csv file. But I wish to automate all this and directly copy a row in the .csv into a column in an existing .csv file.
Here is what i tried:
package require csv
set fp [open "filenameSource.csv" r]
set secondColumnData {}
while {[gets $fp line]>=0} {
if {[llength $line]>0} {
lappend secondColumnData [lindex [split $line ","] 1]
}
}
close $fp
puts $secondColumnData
set filename "Destination.csv"
set fileId [open $filename "w"]
puts -nonewline $fileId $secondColumnData
close
Is there a way to have a pointer at row x in the source file and copy it into a specific destination into the Destination file.
I am new to tcl. Please provide example.
Thanks,
IEK
One thing you'll need to learn as a newcomer to Tcl is that there's a lot of useful code in Tcllib, a suite of packages written by the Tcl community. In this case, the csv and struct::matrix packages make this task trivial (as I understand it), which is great because CSV files have some tricky aspects that aren't obvious.
package require csv
package require struct::matrix
# Read the source data
set srcMatrix [struct::matrix]
set f [open "filenameSource.csv" r]
csv::read2matrix $f $srcMatrix
close $f
# Read the destination data so we can UPDATE it
set dstMatrix [struct::matrix]
set f [open "Destination.csv" r+]
csv::read2matrix $f $dstMatrix
# Leaving the file open; we're going to rewrite it…
# Do the copying operation; I assume you know which row and column to copy from/to
$dstMatrix set column 2 [$srcMatrix get row 34]
# Write back
chan seek $f 0
csv::writematrix $f $dstMatrix
chan truncate $f; # Make sure there's no junk left if the file shortened
close $f

How to create text file with clipboard data

I have copied data to clipboard from my application.
Now I want to store (paste) clipboard data in new notepad file.
I will be using this script in Windows & Linux env.
Please help..
You get the contents of the clipboard with clipboard get. You can then pick a file to save to using tk_getSaveFile, and do the save with open and puts.
package require Tk
wm withdraw .; # Hide the default window
update
set contents [clipboard get]
set filename [tk_getSaveFile -defaultextension .txt]
# Skip saving if we got the empty name; it signifies that the user cancelled
if {$filename ne ""} {
set f [open $filename "w"]
puts $f $contents
close $f
}
exit

in tcl, how to edit string in the open file?

let's say that I have opened a file using:
set in [open "test.txt" r]
I'm intend to revise some string in the certain line, like:
style="fill:#ff00ff;fill-opacity:1"
and this line number is: 20469
And I want to revise the value ff00ff to other string value like ff0000.
What are the proper ways to do this? Thanks in advance!
You need to open the file in read-write mode; the r+ mode is probably suitable.
In most cases with files up to a reasonable number of megabytes long, you can read the whole file into a string, process that with a command like regsub to perform the change in memory, and then write the whole thing back after seeking to the start of the file. Since you're not changing the size of the file, this will work well. (Shortening the file requires explicit truncation.)
set f [open "test.txt" r+]
set data [read $f]
regsub {(style="fill:#)ff00ff(;fill-opacity:1)"} $data {\1ff0000\2} data
seek $f 0
puts -nonewline $f $data
# If you need it, add this here by uncommenting:
#chan truncate $f
close $f
There are other ways to do the replacement; the choice depends on the details of what you're doing.

Writing multiple lines to a file in TCL

I'm looking to modify gpsfeed+ to add in a section which writes the NAV string out to a text file while the simulator is running. The tool is written in tcl and I'm at a loss as to what I need to do. What I have so far is:
if {$prefs(udp) & $::udpOn} {
# opens file to write strings to
set fp [open "input_NAV.txt" w+]
# one sentence per udp packet
foreach line [split $::out \n] {
puts $fp $line
}
close $fp
}
Right now if UDP broadcast is switched on, I want to take each NAV string broadcast over UDP and write it to a file. But the code above only writes 1 of the strings and then overwrites the string. I've been trying to add in a /n switch, but I've not had any joy.
I was using the wrong mode for opening the file:
w+ Open the file for reading and writing. Truncate it if it exists. If it does not exist, create a new file.
I should have been using either of the following:
a Open the file for writing only. If the file does not exist, create a new empty file. Set the file pointer to the end of the file prior to each write.
a+ Open the file for reading and writing. If the file does not exist, create a new empty file. Set the initial access position to the end of the file.
This would be a comment, but formatting.
This code:
foreach line [split $::out \n] {
puts $fp $line
}
Is equivalent to:
puts $fp $::out

Create a new file every time a TCL script runs

I am new to TCL and got some stuff I need to automate and I need my code to log all the commands and results after the login process.
My main issue is that I need to create a distinct log file everytime I run the script and one way I found out was to "append" the unique "timestamp" to the file name.
Here is where it starts to get picky, you see, every time I try to append the variable "$time" to the filename it returns:
couldn't open "15-10-28/11:57:10--xxx.xxxx.xxxx.txt": no such file or directory
while executing
"log_file "$newfile" "
(file "ssh-test.tcl" line 31)
My code is as follows:
set user [lrange $argv 0 0]
set password [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set arg1 [lrange $argv 3 3]
set systemTime [clock seconds]
set time [clock format $systemTime -format %y-%m-%d/%H:%M:%S--]
set a "ssh"
set suffix ".txt"
append newfile "${a}${arg1}${suffix}"
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh $user#$ipaddr
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
log_file "$newfile" ;
expect "*#"
send -- "\r"
send_user "This is the $argv0 Script"
send -- "scm $arg1\r"
expect "*#"
send -- "exit\r"
expect eof
If I use the 'set filename "${a}${arg1}${suffix}"' string and 'log_file "$filename"' it works just fine but it will append the new info to the already existing file and I want a new file everytime I run the script.
If I use the 'append newfile "${a}${arg1}${suffix}"' string and 'log_file "$newfile"' it won't work and return the error already referred.
Hope you guys can help me out and thanks in advance for any support.
You are creating the timestamp with / in it.
set time [clock format $systemTime -format %y-%m-%d/%H:%M:%S--]
While appending this to the variable newfile, it will become 15-10-28/11:57:10--xxx.xxxx.xxxx.txt.
Expect will think that there is a folder called 15-10-28 available and under which I have to create the file 11:57:10--xxx.xxxx.xxxx.txt. Since that folder is not available, you are getting the error message as no such file or directory
After figuring out that the date format was messing up my code, I started playing around with the special characters and got it working like this:
set time [clock format $systemTime -format %a_%b_%d_%Y#%H'%M'%S]
It is not the desired format but at least I got it working as I intended.