Awk with TclX

风行水上 @ 2014-03-12 18:07:50
标签:

    The basic idea behind awk is scan file and respond with the definition of Pattern/Action pair.

    TclX provide a similar way for file scan.

    With this facility you can use Tcl to do the sort of file processing that is traditionally done with awk.

    And since Tcl approach is more declarative, some of the scripts that can be rather difficult to write in awk are simple to code in Tcl.

    package require Tclx
    
    set ctx [scancontext create]
    
    scanmatch $ctx {^Hello} {
      set line $matchInfo(line)
      set name [lindex $line 1]
      puts "Hello $name"
    }
    
    scanmatch $ctx {^Path: } {
      set line $matchInfo(line)
      puts "dir/.. = [file dirname [lindex $line 1]"
    }
    
    scanmatch $ctx {^(continue|stop)} {
      continue  ;# to skip following patterns
      return    ;# to stop the scan
    }
    
    set fp [open $file "r"]
    scanfile $ctx $fp  ;# do the scan job
    close $fp
    
    标签:

      分享到:
      comments powered by Disqus

      27/30ms