Link to home
Start Free TrialLog in
Avatar of pamwestphal
pamwestphal

asked on

TCL Script question

This is a basic basic question, but it is holding me up in my work!! I am new to TCL and just trying to write a script to reformat an input file. I have a file which has (\r\n) at the end of each line. I need to remover the (\n) and leave the (\r).  At the end of the file there should be a (\n).
 
I have this (I didn't put the \n at the end of the file yet):
set in [open data3test.txt]
set out [open test2.txt w]
set data [read $in]
regsub -all {\n} data {} $out
close $out; close $in

Thank you!!
Pam

Avatar of donjon56
donjon56

braces in this case ({}) will cause the '\n' to not be substituted for a newline.

use double-quotes ("\n") to have interpreter perform the substition.

Try

set in [open data3test.txt]
set out [open test2.txt w]
set data [read $in]
regsub -all "\n" data {} $out
close $out; close $in
# or even:
regsub -all \n $data {} $out

but keep in mind that this uses TCL's implementation of \n (which is 0xa usually), if you want to be sure use:

regsub -all \x0a $data {} $out
regsub -all {[\x0a]} $data {} $out
Avatar of pamwestphal

ASKER

Using this code:

set in [open data3test.txt]
set out [open test2.txt w]
set data [read $in]
regsub -all \x0a $data {} $out
regsub -all {[\x0a]} $data {} $out
close $out; close $in

test2.txt is blank
Do I have to do something else?
Pam
Try the following.

set in [open data3test.txt]
set out [open test2.txt w]
set data [read $in]
regsub -all \x0a $data {} data
regsub -all {[\x0a]} $data {} data
puts $out $data
close $out; close $in
donjon56, please take care about your posts (your are missing some $ which makes the suggestions useless, mor or less ..)

> Do I have to do something else?
just one of the regsub is enough
ahoffmann:  I agree that one regsub should be enough.

Did you try my code?

the TCL syntax is "regsub ?switches? exp string subSpec ?varName?"

$ only provides the data that is stored in the variable

the string argument is provided by using $data (the value stored in data)
the varName argument is provided by using data (the name of the variable we want to store the new string)

notice there is no channelId shown for the regsub command.

when she puts $out in the regsub spot she is passing the channelid to the regsub.  So the regsub creates a variable named channelid and stores the substitution there, instead of writing it out to the file like she expects.
ASKER CERTIFIED SOLUTION
Avatar of donjon56
donjon56

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial