Posted to tcl by mjanssen at Thu Oct 25 00:37:15 GMT 2007view raw

  1. set re {^$}
  2. fconfigure stdout -encoding binary -translation binary -buffering full
  3.  
  4. proc filter {argv re} {
  5. foreach file $argv {
  6. set f [open $file r]
  7. fconfigure $f -encoding binary -translation binary -buffersize 8096
  8. set buffer ""
  9. while {![eof $f]} {
  10. while {[string first \n $buffer] == -1} {
  11. append buffer [read $f 8096]
  12. }
  13. set bl [split $buffer \n]
  14. foreach line [lrange $bl 0 end-1] {
  15. if {[regexp -- $re $line]} {puts $line}
  16. }
  17. set buffer [lindex $bl end]
  18. }
  19. close $f
  20. }
  21. }
  22. filter $argv $re

Comments

Posted by willdye at Fri Oct 26 21:23:59 GMT 2007 [text] [code]

mcvoy noted on the Tcl Chat that this script is currently 3.6 times slower than the roughly equivalent Perl script: $re='^$'; while(<>){ print if /$re/0;}