2016-12-27 42 views
0

如何从行尾或最大行[expr max_line - 3]获取第3行中的字符串,我已经写下了下面的代码,但是我无法通过第3行从最后一行或最大行中得到结果。获取文件中的行

set idx 0 
while {![eof $flopen]} { 
    gets $flopen line 
    puts $line 
    set vlist [split $line " "] 
    set vle [string trim [lindex $vlist 0]] 
    if {$vle == "STP"} { 
    set dtxid [string trim [lindex $value_list 1]] 
    set dtid [string trim [lindex $value_list 4]] 
    gets $flopen line 
    gets $flopen line 
    gets $flopen line 
    set line [join $line ","] 
    set tglist($idx) $dtxid 
    set gslist($idx) $dtid 
    set atblist($idx) $line 

    set data_end_from_max_line $datax ;# Can set the string here [expr $max_line - 3] 

    incr idx 
} 
} 

回答

1

在做这样的事情,最简单的方法(提供的数据不是太大,所以不超过几百兆字节)是加载这一切并处理它里面的Tcl作为行列表。

set lines [split [read $flopen] "\n"] 
set particularLine [lindex $lines end-3] 
+0

那是完美的,但我必须重新打开该文件,不是正确的?!我的意思是说只能一个人打开排队呢? – Andre

+0

由于我们从文件末尾向后计数,因此我们不希望列表中的虚假空最后一个元素。最好做'设置行[split [read -nonewline $ flopen] \ n]' –