使用Tcl从文件中提取字符串

时间:2013-12-06 12:51:13

标签: tcl

我有一个文件index.txt,其中包含PC名称[比如temp [0]等]和性能参数[比如说2.3等],以表格形式排列为

PC  Memory  Processor  Bus  Performance . 

我想要一个tcl文件只提取字符串PC和性能值。

请给我一些建议。

1 个答案:

答案 0 :(得分:1)

您需要来自tcllibtextutil::split个包。

package require textutil::split
set fid [open index.txt r]
while {[gets $fid line] != -1} {
    lassign [textutil::split::splitx $line] pc memory processor bus performance
    puts "$pc  $performance"
}

如果您不想使用tcllib,可以这样做:

while {[gets $fid line] != -1} {
    set words [regexp -all -inline {\S+} $line]
    if {[llength $words] == 7} {
        puts "[lindex $words 0]  [lindex $words 4]"
    }
}