Prev: multiple files into one file based on unique entry in one of the files
Next: How to change the font and font size in ttk::button (themed widgets)?
From: Glenn Jackman on 17 Mar 2010 09:03 At 2010-03-17 08:47AM, "Cesear" wrote: > I have 4 flat files where each field is separated by a pipe |. In > each file the second field has a unique value that is in all the > files. Each line is terminated by a newline. I what to combine each > file and create one file. In this one file, there should be one line > for each "unique" entry in that was found in the second file. I was > able to do this in MS access by creating each file as table and > linking the "unique" field in the 2nd file to the other files. I want > do this in Tcl, so I can automate the process. Come some please point > me in a starting direction? I know enough Tcl to get by, but not much > in the I/O region. Any ideas or would be great! > > Thx!! I don't understand what you want as the result. Is it: 1. a single output file containing all of the 4 original files 2. several output files depending on the 2nd field of each line of the 4 original files If it's #2, array set output {} foreach file {file1 file2 file3 file4} { set input [open $file r] while {[gets $input line] != -1} { set field2 [lindex [split $line |] 1] if { ! [info exists output($field2)]} { set output_filename [format "output_%s.out" $field2] # ... or whatever you want the output file to be named set output($field2) [open $output_filename w] } puts $output($field2) $line } close $input } foreach item [array names output] {close $output($item)} -- Glenn Jackman Write a wise saying and your name will live forever. -- Anonymous
From: Glenn Jackman on 17 Mar 2010 09:05
At 2010-03-17 09:02AM, "Arjen Markus" wrote: > while {[gets $infile line] } { Note, should be while {[gets $infile line] != -1} { -- Glenn Jackman Write a wise saying and your name will live forever. -- Anonymous |