• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

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

0
pamwestphal
Asked:
pamwestphal
  • 4
  • 2
1 Solution
 
donjon56Commented:
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
0
 
ahoffmannCommented:
# 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
0
 
pamwestphalAuthor Commented:
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
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
donjon56Commented:
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
0
 
ahoffmannCommented:
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
0
 
donjon56Commented:
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.
0
 
donjon56Commented:
FYI:  official documentation:
http://www.tcl.tk/man/tcl8.4/TclCmd/regsub.htm
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now