tongtong
tongtong

Reputation: 47

Tcl How to modify csv and it can control another file's data without using tcllib

I have many files of process and it contain versions, I have regexp certain line of them and import them into a txt file , the txt format is like

   #process #AA_version       #BB_version
   a11      Aa/10.10-d87_1    Bb/10.57-d21_1
   a15      Aa/10.15-d37_1    Bb/10.57-d28_1
   a23      Aa/10.20-d51_1    Bb/10.57-d29_3

and then I change the txt to csv format like

,#process,#AA_version,#BB_version
,a11,Aa/10.10-d87_1,Bb/10.57-d21_1
,a15,Aa/10.15-d37_1,Bb/10.57-d28_1
,a23,Aa/10.20-d51_1,Bb/10.57-d29_3

And now I want to write a tcl(get_version.tcl) it can generate the corresponding version (in the file where I regexp the version) after parsing the csv file

ex. If I tclsh get_version.tcl to parse csv file, input the process I want to modify(a11) and it will puts the current version AA_version: Aa/10.10-d87_1 BB_version: Bb/10.57-d21_1 and I can modify the version and the get_version.tcl can update the csv file and the file (where I regexp) at the same time.

Is that available? and I cannot install tcllib , can I do these thing without tcllib? It contains too much commands, I don't know how to start, Thanks for help~

If I want to change the AA_version of process a11, and use

get_version.tcl a11 
AA_version = **Aa/10.10-d87_1** 
BB_version = **Bb/10.57-d21_1**

It will read the csv file and add the new version(ex.Aa/10.13-d97_1 ), and this action will change the file (1.add a new line AA_version =Aa/10.13-d97_1 2. modify AA_version = Aa/10.10-d87_1 ->#AA_version = Aa/10.10-d87_1) in original file

Upvotes: 0

Views: 174

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137717

I'd process each line something like this:

split [regsub -all {\s+} $line "\u0000"] "\u0000"

(The tcllib textutil::splitx command does something similar.)

If you don't have commas or quotes in your input data — and it looks like you might be able to guarantee that easily — then you can just join $record "," to get a line you can write out with puts. If you do have commas or quotes or stuff like that, use the Tcllib csv package because that handles the tricky edge cases correctly.


As a simple stdin→stdout filter, the script would be:

while {[gets stdin line] >= 0} {
    set data [split [regsub -all {\s+} $line "\u0000"] "\u0000"]
    # Any additional column mangling here
    puts [join $data ","]
}

Upvotes: 1

Related Questions