2016-12-27 44 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]' –