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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing and manipulating column based text file in tcl/tk

Status
Not open for further replies.

Agg38

Technical User
May 17, 2011
3
US
Hi All,

I am new to tcl/tk programming and need some help on reading and manipulating particular columns in text files. What I need to do is read a text file which has 5 columns of number and add or subtract a user defined number from the last column's value. For example the text file is something like following, and I need to subtract the fifth column values from a user provided number(e.g 70) and write all the columns into a new file.

0 1 300 150 53.57
1 2 5357.5 150 23.1825
2 3 10515 100 30.69992
3 4 15672.5 100 26.03
4 5 20830 200 22.1319
5 6 200 2650 22.15530
6 8 5357.5 2650 28.03
7 9 10515 2650 32.6376
8 0 15672.5 2650 28.9956
 
To read a file, named, say, "d:/abc/def/hij.txt":
Code:
set fid [open d:/abc/def/hij.txt r]
set lst_lines [split [read $fid] \n]
close $fid
Now, this will give you a list, lst_lines where each element is a line from the file. To loop through those lines and create a list of columns (fields) at each iteration:
Code:
set fid [open $newfilename w]
foreach str_line $lst_lines {
    set lst_clmns [split $str_line]
    set lst_clmns [lreplace $lst_clmns 4 4 [expr{70-[lindex $lst_clmns 4]}]]
    puts $fid [join $lst_clmns " "]
}
close $fid
where "set lst_clmns [lreplace $lst_clmns 4 4 [expr{70-[lindex $lst_clmns 4]" replaces the 5 element (index 4) with 70 minus what was there before.

To

_________________
Bob Rashkin
 
Hi Bong, Thanks for the reply. When I try your code it gives an error of "invalid command name expr{70-23.119}" and generates an empty file. Do you know why I must be getting this error when I try to execute the "set lst_clmns [lreplace $lst_clmns 4 4 [expr{70-[lindex $lst_clmns 4]" command
 
I figured out the reason of error. But now I have one more task, now I need to add two text files together and write the output to a new file. I have to add the column4 of file1 to column4 of file2 put the output(all the columns) in a new file.
For example:

file1:
0 0 200 200 -45.21225200000001
1 0 5357.5 200 -11.640779300000005
2 0 10515 200 4.7480759999999975


file2:
0 0 200 200 23.212252
1 0 5357.5 200 25.6407793
2 0 10515 200 25.251924

and the file3 output should be:
0 0 200 200 -21.99998
1 0 5357.5 200 14.0
2 0 10515 200 30.2011735

 
Are the records in the 2 files guaranteed to be synchronized? That is, will there be any checking to determine which record of file-1 gets correlated to which record of file-2, or just marching along in lock step?

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top