我很困惑地逐行编辑我的文本文件。如何逐列排序,增加列树关键字条件(列树:23,2,53,n ..)。
set fp [open "data.txt" r]
set filecontent [read $fp]
Text file content:
one, cat, 23,yes,check
two, zebra, 2,yes,check
tree, bird, 53,yes,check
fourth, dog, 15,no,uncheck
five, worm, 9,no,uncheck
six, monkey, 41,yes,uncheck
and so on..
Output text file content:
two, zebra, 2,yes,check
five, worm, 9,no,uncheck
fourth, dog, 15,no,uncheck
one, cat, 23,yes,check
six, monkey, 41,yes,uncheck
tree, bird, 53,yes,check
谢谢!
答案 0 :(得分:1)
让列表完成所有工作。使用[lsort -integer -index 2 $lines_list]
其中lines_list
是行列表,每行也是列表。 Tcl将按第三个整数子列表元素对子列表进行排序。
set fp [open "data.txt" r]
set filecontent [read $fp]
close $fp
set text [split $filecontent \n]
foreach line $text {
lappend lines_list [split $line ","]
}
set lines_list [lsort -integer -index 2 $lines_list]
set text ""
foreach line $lines_list {
append text [join $line ","] \n
}
set fout [open "out.txt" w]
puts -nonewline $fout $text
close $fout