【问题标题】:Delete all line before and after a pattern is found in tcl删除在 tcl 中找到模式之前和之后的所有行
【发布时间】:2015-11-06 12:14:28
【问题描述】:

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

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 的新手。在这里需要一些帮助。 非常感谢。

【问题讨论】:

    标签: tcl


    【解决方案1】:

    这使用“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
    

    【讨论】:

      【解决方案2】:

      更面向行的替代方案

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-07
        • 2021-02-13
        • 1970-01-01
        • 2015-02-20
        • 2012-03-15
        • 2020-09-27
        • 2022-01-07
        • 1970-01-01
        相关资源
        最近更新 更多