Tcl对文件进行排序 [英] tcl sort a file
本文介绍了Tcl对文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个目录,其中包含名为*MIS的多个文件。现在,这些文件包含重复的行。 我需要读取名为VIA的目录中的所有文件,名称为*_MIS&;删除重复行后更新文件(需要执行Sort-u)。需要使用Tcl
推荐答案
如果您是在纯Tcl中执行此操作,则应将任务拆分为两部分:一部分用于对文件进行排序(这将是一个很好的过程),另一部分用于对目录中的所有相关文件进行排序:
proc sort_file {filename} {
# Read the file
set f [open $filename]
set data [read $f]
close $f
# Sort the lines, removing dupes
set lines [split $data "
"]
set sorted_uniques [lsort -unique $lines]
set data [join $sorted_uniques "
"]
# Write the file
set f [open $filename w]
puts $f $data
close $f
}
# Now process all the relevant files; the -directory option is a convenient way
# to specify which directory to do the searching in.
foreach filename [glob -directory VIA *_MIS] {
sort_file $filename
}
解决方案的关键部分:lsort -unique
和foreach
/glob
。
这篇关于Tcl对文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文