删除在tcl中找到模式之前和之后的所有行

时间:2015-11-06 00:14:15

标签: tcl

我想在名为“Summary.txt”的自动生成文件中找到模式之前和之后删除所有行。     1)在找到“结果摘要”表达式之前删除所有行。     2)在找到结果“结束结束”后删除所有行。

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX Here are some unwanted lines XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Summary of Result
Line 2: Grammar error
Line 14: Missing of punctuations "!"
Line 15: Spelling error
Line 21: Spelling error
Line 40: Missing of punctuations ","

End of Result
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX Here are some unwanted lines XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

我是TCL的新手。需要一些帮助。 非常感谢你。

2 个答案:

答案 0 :(得分:2)

这使用" fileutil" (来自Tcllib)读取和写入文件" regexp"提取所需的文字:

package require fileutil
set data [::fileutil::cat Summary.txt]
if {![regexp "Summary of Result.*End of Result\n" $data result]} {
    error "Expression not found."
}
::fileutil::writeFile Output.txt $result

答案 1 :(得分:1)

更多面向行的替代方案

set fid [open Summary.text]
set in_result false
while {[gets $fid line] != -1} {
    if {[string match "*Summary of Result*" $line]} {set in_result true}
    if {$in_result} {puts $line}
    if {[string match "*End of Result*" $line]} break
}
close $fid
相关问题