Editing file in TCL and writing the revised contents to an output file

十年热恋 提交于 2020-01-17 03:43:07

问题


I have an input file fI as follows :-

module c1

accent {a1} {Z} ;# net:n1551 , little:big
accent {a2} {C} ;# net:n1536 , little:big

module c2

accent {a3} {Z} ;# net:n1552 , little:big
accent {a4} {C} ;# net:n1537 , little:big

I want to manipulate this input file and get the output file fO as follows:-

accord [get_pins c1/a1/Z] [get_nets c1/n1551]
accord [get_pins c1/a2/C] [get_nets c1/n1536]

accord [get_pins c2/a3/Z] [get_nets c2/n1552]
accord [get_pins c2/a4/C] [get_nets c2/n1537]

How can i do this in TCL ? Please help .


回答1:


Assuming that "fI" and "fO" are actual file names:

set fi [open fI]
set fo [open fO w]
set module {}
while {[chan gets $fi line] >= 0} {
    if {[string is space $line]} continue

    switch -- [lindex $line 0] {
        module {set module [lindex $line 1]}
        accent {
            if {[regexp {{(\w+)}.+{(\w+)}.+:(\w+) ,} $line -> a b c]} {
                chan puts $fo "accord \[get_pins $module/$a/$b] \[get_nets $module/$c]"
            }
        }
    }
}
chan close $fi
chan close $fo

Documentation: append, foreach, if, proc, puts, set, split, string



来源:https://stackoverflow.com/questions/31077136/editing-file-in-tcl-and-writing-the-revised-contents-to-an-output-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!