Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCL join 2 columns in a file

Status
Not open for further replies.

UCPR

Technical User
Oct 14, 2014
3
US
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 -->
2
5
8
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.

Thanks,
UCPR


 
Hi

Probably not what you need, but matches exactly your data transformation sample :
Code:
[b]set[/b] fin [COLOR=teal][[/color][b]open[/b] [i][green]"UCPR.txt"[/green][/i] r[teal]][/teal]
[b]set[/b] fout [COLOR=teal][[/color][b]open[/b] [i][green]"UCPR-out.txt"[/green][/i] w[teal]][/teal]

[b]while[/b] [teal]{[/teal] [COLOR=teal][[/color][b]gets[/b] [navy]$fin[/navy] line[teal]][/teal] [teal]>=[/teal] [purple]0[/purple] [teal]}[/teal] [teal]{[/teal]
    [b]puts[/b] [navy]$fout[/navy] [COLOR=teal][[/color][b]lrange[/b] [COLOR=teal][[/color][b]split[/b] [navy]$line[/navy][teal]][/teal] [purple]1[/purple] end[teal]][/teal]
[teal]}[/teal]

[b]close[/b] [navy]$fout[/navy]
[b]close[/b] [navy]$fin[/navy]

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.


Feherke.
feherke.ga
 
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!

Regards,
UCPR
 
 http://files.engineering.com/getfile.aspx?folder=197e5666-272a-4789-a394-a0ed9e2bf08f&file=try2.tcl
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top