需要使用tcl脚本从文本文件中打印两个不同的字符串

时间:2013-09-19 05:56:36

标签: tcl

我要求使用tcl脚本从文件的两个连续行中获取两个不同的字符串 我试过跟随,但它不起作用。 所以下面我需要打印字符串“Clock”和“b0”。我能打印时钟。但我需要“时钟”“b0”

set f [eval exec "cat src.txt"]
set linenumber 0

while {[gets $f line] >= 0} {
    incr linenumber
    if {[string match "Clock" $line] >= 0 } {
        # ignore by just going straight to the next loop iteration
        while {[gets $f line] >= 0} {
            incr linenumber
            if { [string match "b0" $line"]} {
                close $out
                puts "final $line"
            }      
            puts "\n$line"
            continue
        }
    }
}
close $f

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

set f [open "src.txt" r]; # opening file

while {![eof $f] >= 0} {
    set line [gets $f]; # reading line from file
    if {[string match "*Clock*" $line]} {
        ; # if Clock found
        puts $line
        set line [gets $f]; # reading next line from file
        if { [string match "*b0*" $line]} {
            ; # if b0 found
            puts "final $line"
        }
    } 
}
close $f