如何在tcl中打印新更新的行

时间:2015-04-08 06:59:22

标签: tcl

我需要打印文件中新添加的行。 我的代码如下:

proc dd {} {
    global line_number
    set line_number 0
    set a [open "pkg.v" r]
    #global count
    while {[gets $a line]>=0} {
        incr line_number
        global count
        set count [.fr.lst2 size]
        puts "enter $count"
        if {[eof $a]} {
            #.fr.lst2 insert end "$line"
            # set count [.fr.lst2 size]
            close $a
        } elseif {$count > 0} {
            .fr.lst2 delete 0 end
            if {$count+1} {
                .fr.lst2 insert end "$line"
                puts "i am $count"
            }
        } else {
            .fr.lst2 insert end "$line"
            puts "i am not"
        }
    }
    puts "$count"
}

1 个答案:

答案 0 :(得分:0)

假设我们正在谈论在任何Unix系统(Linux,OSX等)上写入日志文件末尾的行,那么在tail的帮助下,这很简单:

# Make the pipeline to read from 'tail -f'; easy easy stuff!
set mypipe [exec |[list tail -f $theLogfile]]
# Make the pipe be non-blocking; usually a good idea for anything advanced
fconfigure $mypipe -blocking 0
# Handle data being available by calling a procedure which will read it
# The procedure takes two arguments, and we use [list] to build the callback
# script itself (Good Practice in Tcl coding)
fileevent $mypipe readable [list processLine $mypipe .fr.lst2]

proc processLine {pipeline widget} {
    if {[gets $pipeline line] >= 0} {
        # This is probably too simplistic for you; adapt as necessary
        $widget insert end $line
    } elseif {[eof $pipeline]} { # Check for EOF *after* [gets] fails!
        close $pipeline
    }
}
相关问题