?
Solved

TCL Script question

Posted on 2007-07-23
7
Medium Priority
?
376 Views
Last Modified: 2013-11-22
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
Comment
Question by:pamwestphal
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:donjon56
ID: 19569518
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19572820
# 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
 

Author Comment

by:pamwestphal
ID: 19574700
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:donjon56
ID: 19576471
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19577982
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
 
LVL 4

Expert Comment

by:donjon56
ID: 19578531
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
 
LVL 4

Accepted Solution

by:
donjon56 earned 2000 total points
ID: 19578562
FYI:  official documentation:
http://www.tcl.tk/man/tcl8.4/TclCmd/regsub.htm
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question