Updated 2003-08-26 18:11:25

Contributed by glennj

Works like "tail -f aFile | grep aPattern" should work.
  #!/bin/sh
  #\
  exec tclsh "$0" ${1+"[email protected]"}

  proc main {argv0 argc argv} {
      if {$argc != 2} {
          puts "$argv0 <regex> <file>"
          exit
      }

      foreach {regex filename} $argv break

      if {![file readable $filename]} {
          puts stderr "can't read '$filename'"
          exit 1
      }

      set pipe [open "| tail -f $filename" r]
      set ::pid [pid $pipe]
      puts "\[tail process started (pid $::pid)]"

      fileevent $pipe readable [list inputHandler $pipe $regex]

      vwait ::forever
  }

  proc inputHandler {channel re} {
      if {[gets $channel line] == -1} {
          if {[eof $channel]} {
              puts "\[tail process died (pid $::pid)]"
              set ::forever now
          }
      } elseif {[regexp -- $re $line]} {
          puts $line
      }
  }

  main $argv0 $argc $argv