Reputation: 21
I am new to tcl scripting.
I have 2 columns in my CSV file.
Example: My data
A 12
D 18
B 33
A 11
D 49
I would like to extract column 2 values using Column 1 data.
Required output:
A: 12,11
B: 33
D: 18, 49
Can someone please help me to write a tcl script to perform this task? Thank you in advance.
Upvotes: 0
Views: 220
Reputation: 137717
This is a very well known problem, and Tcl's dict lappend
is exactly the tool you need to solve it (together with csv::split
from tcllib to do the parsing of your input data).
package require csv
# Read split and collect
set data {}
while {[gets stdin line] >= 0} {
lassign [csv::split $line] key value; # You might need to configure this
dict lappend data $key $value
}
# Write out
puts ""; # Blank header line
dict for {key values} $data {
puts [format "%s: %s" $key [join $values ", "]]
}
The above is written as a stdin-to-stdout filter. Adapting to work with files is left as an exercise.
Upvotes: 2