你好,我需要一些帮助来将一个文件的内容复制到另一个文件,除了 tcl 中一些损坏的行
Hello, I need some help to copy the content of one file to another except from some corrupted lines in tcl
我有一个包含几千行的文件,格式如下:
1.3.111.2.802.1.1.3.1.6.3.1.2.5.1.2 2 5
好吧,在这种情况下,我们可以尝试将每一行视为一个列表;这些行似乎 well-formed 足够了(第一个字段是 OID 吗?)
while {[gets $inChannel line] >= 0} {
if {[llength $line] <= 1 || [tcl::mathop::<= 0 [lindex $line end] 8191]} {
puts $outChannel $line
}
}
这里比较棘手的是 tcl::mathop::<=
的使用,它是 <=
表达式运算符的命令形式,它允许我们检查值(从行的最后一个字开始) 在 0 到 8191 的范围内,无需重复。
更谨慎的做法是:
while {[gets $inChannel line] >= 0} {
if {[catch {llength $line} length] || $length <= 1} {
# Ill-formed and short lines get copied
puts $outChannel $line
continue
}
set value [lindex $line end]
if {![string is integer -strict $value]} {
# Lines with non-integers get copied
puts $outChannel $line
continue
}
if {[tcl::mathop::<= 0 $value 8191]} {
# Lines with values in range get copied
puts $outChannel $line
}
}
可以不重复 puts
但我认为生成的代码不太清楚。
这使用正则表达式来捕获最后一个点之后的数字,而不是 split
s 和 lindex
s
set in [open $inputfile r]
set out [open $output w]
while {[gets $in line] != -1} {
if {[string match {CKSUM*} $line]} then continue
# capture the digits following the last dot
if {[regexp {.*\.(\d+)} $line -> key] && 0 <= $key && $key <= 8919} {
puts $out $line
}
}
close $in
close $out
我有一个包含几千行的文件,格式如下:
1.3.111.2.802.1.1.3.1.6.3.1.2.5.1.2 2 5
好吧,在这种情况下,我们可以尝试将每一行视为一个列表;这些行似乎 well-formed 足够了(第一个字段是 OID 吗?)
while {[gets $inChannel line] >= 0} {
if {[llength $line] <= 1 || [tcl::mathop::<= 0 [lindex $line end] 8191]} {
puts $outChannel $line
}
}
这里比较棘手的是 tcl::mathop::<=
的使用,它是 <=
表达式运算符的命令形式,它允许我们检查值(从行的最后一个字开始) 在 0 到 8191 的范围内,无需重复。
更谨慎的做法是:
while {[gets $inChannel line] >= 0} {
if {[catch {llength $line} length] || $length <= 1} {
# Ill-formed and short lines get copied
puts $outChannel $line
continue
}
set value [lindex $line end]
if {![string is integer -strict $value]} {
# Lines with non-integers get copied
puts $outChannel $line
continue
}
if {[tcl::mathop::<= 0 $value 8191]} {
# Lines with values in range get copied
puts $outChannel $line
}
}
可以不重复 puts
但我认为生成的代码不太清楚。
这使用正则表达式来捕获最后一个点之后的数字,而不是 split
s 和 lindex
s
set in [open $inputfile r]
set out [open $output w]
while {[gets $in line] != -1} {
if {[string match {CKSUM*} $line]} then continue
# capture the digits following the last dot
if {[regexp {.*\.(\d+)} $line -> key] && 0 <= $key && $key <= 8919} {
puts $out $line
}
}
close $in
close $out