TTING
Written by Wojciech Kocjan   
Recently I was working with various texts that I wanted to convert or check things in. As a long-time Unix user, I start with grep, awk and things like that. Usually one or more greps will give you what you find and wc will also do statistics for you, but there are a couple of problems. In this article I'll explain what they are and how I'm planning on using Tcl now for the same thing.

First of all, most people use these tools in a fire-and-forget mode - which means that you go over a try-run-correct sequence several times until you get what you expected to see. After you have all your results, your tiny command is probably still somewhere in your shell history. After a month it detinitely isn't anymore. Even if you saved the command as shell script, you probably hacked it so that you're not even sure how to run it.

That's what I get with most of my hackery scripts. Often I'm also thinking that doing the same in Tcl isn't probably much harder, but all the remaining code to read text files (and the dilemma whether to use fileutil package from Tcllib or just do everything manually is hard. What if I then want to run it on a box without Tcllib? All in all it's just faster to run grep and get the job done.

After I did my last set of scripts I figured that there has to be an alternative. At some point I wanted small subset of awk, but I wanted to skip using awk at the same time since it's much more complicated. And what I actually hoped for is to have something similar to Visual Regexp, where you can try your expressions in a GUI and have a nice environment to work on instead of a command line.

Then I started coding a small tool, it even worked nicely, added a GUI, open/save options and in less than 300 lines I got a complete tool that does this thing. And you can even use it from command line, if you need to. Below is a screenshot of the GUI along with an example - parsing copy+paste from amazon.com pages that creates a list of items. You can then import it into Excel or OpenOffice Calc.

TTING


What the tool does is allow you to quickedit Tcl code per line or that gets executed before/after per-line code and then you can pretty much use it like a more powerful grep. In the example above it reads first part of the information, stores it and when it finds second part of the information, it prints out the complete data. As rough as it seems, it does the job done in almost all cases.

Another possibility is to use the command line. It allows specifying one more Tcl scripts to run and process one or more files. A typical example might be to

Running tting
$ ./tting.tcl -script 'add [string toupper $item]' /etc/motd
LINUX UBUNTU1 2.6.24-19-SERVER #1 SMP WED AUG 20 23:54:28 UTC 2008 I686

I will describe details of how the code is implemented in a separate article. It will also contain an updated version of the script, since it is far from complete. I hope it will be under 500 lines when it's done, though.

Download the source code here: tting-0.1.zip.