Jorge Moreno 2006-02-10 - Here is a frontend for LogParser
http://www.microsoft.com/technet/scriptcenter/tools/logparser/default.mspx <<< not there anymore

  • Of course, windows only
  • Very incomplete, proof of concept only

Very powerful text file parser:

  • SQL like query definition
  • output to a file, console, chart, Database
  • multiple text files treated as a single set of data
  • handles various input file types (CSV, TSV, XML,etc)
  • much more...


Step 1, Get the sample data:

 file: us-500.zip (which contains us-500.csv)

Make sure you convert the sample file to DOS format CR+LF (Its original format is Unix format so LF only for line termination)
You can do that by opening in excel and then overwriting to CSV format, or change the format with your favorite text editor.

Step 2, Prepare your query in a separate file
filename: query.sql, contents:

 SELECT first_name,last_name,city FROM us-500.csv WHERE state = 'CA'

Dependecies in this example:

  • the path to LogParser is hard-coded "C:\Program Files (x86)\Log Parser 2.2"
  • the default query file called "query.sql" mentioned above

You will see some code being reused from this wiki (like RS's proc e'get) used in e: a tiny editor plugin for eTcl

console show
set types {*.sql}

proc invoke command {
global fh
    set fh [open |$command r+]
    set stdout [read $fh]
    set stderr {}
    set status [catch {close $fh} stderr e]
    if {$status} {
        dict with e {}
        lassign [set -errorcode] sysmsg pid exit
        if {$sysmsg eq {NONE}} {
            #output to stderr caused [close] to fail.  Do nothing
        } elseif {$sysmsg eq {CHILDSTATUS}} {
            return [list $stdout $stderr $exit]
        } else {
            return -options $e $stderr 
    return [list $stdout $stderr 0]

proc e'get name {
  if [file exists $name] {
     set f [open $name]
     set ::g(filename) $name
     K [read $f] [close $f]
  } else {corp $name}

proc K {a b} {set a}

proc showSQL sqlFile {
.ctrls.qryPath delete 0 end
.ctrls.qryPath insert end $sqlFile
.ctrls.sql delete 1.0 end
   foreach line [split [e'get $sqlFile] \n] {
     .ctrls.sql insert end $line\n

proc Run {} {
  global fh log but command header query
  puts [pwd]
 $log delete 1.0 end
 set results [invoke "\"C:/Program Files (x86)/Log Parser 2.2/LogParser.exe\" \
 -i:CSV \
 \"[.ctrls.sql get 1.0 end]\" \
 $log insert end "[lindex $results 0]"
 $log insert end "[lindex $results 1]"
 #$log see end

frame .t
frame .ctrls
set log [text .t.log -width 80 -height 30 \
  -borderwidth 2 -relief raised -setgrid true \
  -yscrollcommand {.t.scroll set}]
scrollbar .t.scroll -command {.t.log yview}
canvas .c -height 20 -width 640
set but [button .ctrls.run -text "run" -command Run]
button .ctrls.qry -text "Select Query..." -command {
 set query [tk_getOpenFile -filetypes [list   [list "All files" $types]]]
 showSQL $query
entry .ctrls.qryPath -width 50
button .quit -text "exit" -command exit
text .ctrls.sql -width 40 -height 10

pack .quit
pack .t.scroll -side right -fill y
pack .t.log -side left -fill both -expand true
pack .t -side top -fill both -expand true
pack .c
grid .ctrls.qry .ctrls.qryPath
grid .ctrls.run .ctrls.sql
pack .ctrls

#default header file
set header "[pwd]/header.txt"
#default query file
set query "[pwd]/query.sql"

if [file exists $query] {
   .ctrls.qryPath insert end $query
   showSQL $query
} else {
       tk_messageBox -message "There is no default query file defined\n
                             ($query was expected)"

See also:

Fetching backrefs...