Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


TCL join 2 columns in a file

TCL join 2 columns in a file

TCL join 2 columns in a file

Hi Guys,

I am trying to read 2 columns from a file and print that out into another file, after some data manipulation.
Eg: The input file would look like this
Col1 Col2 Col3
1 2 3
4 5 6
7 8 9
Output should be
Col2 Col3
2 3
5 6
8 9

So far, I was able to read Col2 and Col3 as lists and later converted it into 2 different columns but I can't get to join the 2 columns in a single file.
For eg: puts $Col2 yields the following -->
Similarly for $Col3
Can someone on this forum please help? How do I merge the 2 columns with a space as delimiter? I tried using "format", "concat" and "join", but none of these worked for me.


RE: TCL join 2 columns in a file


Probably not what you need, but matches exactly your data transformation sample :

CODE --> Tcl

set fin [open "UCPR.txt" r]
set fout [open "UCPR-out.txt" w]

while { [gets $fin line] >= 0 } {
    puts $fout [lrange [split $line] 1 end]

close $fout
close $fin 

So please post your existing code and give us details about the processing you need. There I handled the data in the less memory consuming way, keeping in memory only the currently processed line. But for certain calculations you may need to list of entire row, list of entire column or matrix of entire data. But we have to know details to be able to suggest the optimal way.


RE: TCL join 2 columns in a file

Thanks Feherke!

I understand when you say your prog. gets me the example o/p that I provided in my original post. It spits out the i/p file without the first col. But what I am really doing is picking 2 cols (Col9, Col15) from a data set which has 15 columns. I had to do list manipulation to get there (I started writing tcl a couple of days back and unfortunately got only to this level so far). I am attaching my code (try2.tcl) along with input file (FILE2.dat), if you would like to take a look.
The o/p from my current program joins 2 cols but not on a row-by-row basis. It skews up the correspondence between cols. In my example, I would like the o/p to look like this:
1234 first_case
5678 second_case

where 1234 and 5678 are not strings (I will be doing some basic math operations on that data and printing those as %f)

Appreciate your help in this!


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close