A Flock of awks
There are also several commercial versions of awk.
In this section, we review the ones that we know about.
Mortice Kern Systems (MKS) in Waterloo, Ontario (Canada)
awk as part of the MKS Toolkit for MS-DOS/Windows, OS/2, Windows 95,
and Windows NT.
The MKS version implements POSIX awk. It has the following extensions:
if given no
An additional function
is available. This function takes
a string argument, and returns the numeric value of the first character
in the string. It is similar to the function of the same name in Pascal.
Thompson Automation Software
makes a version of awk (tawk)
for MS-DOS/Windows, Windows 95 and NT, and Solaris.
Tawk is interesting on several counts.
First, unlike other versions of awk, which are interpreters, tawk
is a compiler.
Second, tawk comes with a screen-oriented debugger, written in awk!
The source for the debugger is included.
Third, tawk allows you to link your compiled program with arbitrary
functions written in C.
Tawk has received rave reviews in the
Tawk comes with an
interface that acts like POSIX awk,
compiling and running your program.
You can, however, compile your program into a standalone executable file.
The tawk compiler actually compiles into a compact intermediate form.
The intermediate representation is linked with a library that executes
the program when it is run, and it is at link time that other C routines
can be integrated with the awk program.
Tawk is a very full-featured implementation of awk. Besides implementing
the features of POSIX awk (based on new awk), it extends the language
in some fundamental ways, and also has a very large number of built-in
This section provides a "laundry list" of the new features in tawk.
A full treatment of them is beyond the scope of this book; the tawk
documentation does a nice job of presenting them.
Hopefully, by now you should be familiar enough with awk that the value of
these features will be apparent.
Where relevant, we'll contrast the tawk feature with a comparable
feature in gawk.
Additional special patterns,
, but the
actions in its procedure are run before
those of the
provide you the ability to have per-file
start-up and clean-up actions.
Unlike using a rule based on
FNR == 1
, these actions are executed even when files are empty.
Controlled regular expressions. You can add a flag to a regular
expression ("/match me/") that tells tawk how to treat the regular expression.
flag ("/match me/i") indicates
that case should be ignored when doing matching.
flag indicates that the shortest possible
text should be matched, instead of the longest.
] statement. This is similar to
except that tawk exits immediately, bypassing any
, if provided, becomes the return value from tawk to its
True multidimensional arrays. Conventional awk simulates multidimensional
arrays by concatenating the values of the subscripts, separated by the
, to generate a (hopefully) unique index in a regular
associative array. While implementing this feature for compatibility, tawk
also provides true multidimensional arrays.
a = "hello"
a = "world"
for (i in a)
Multidimensional arrays guarantee that the indices will be unique, and
also have the potential for greater performance when the number of elements
gets to be very large.
Automatic sorting of arrays. When looping over every element of an array using
for (item in array)
construct, tawk will first sort the indices
of the array, so that array elements are processed in order. You can
control whether this sorting is turned on or off, and if on, whether the
sorting is numeric or alphabetic, and in ascending or descending order.
While the sorting incurs a performance penalty, it is likely to be less
than the overhead of sorting the array yourself using awk code, or piping
the results into an external invocation of
Scope control for functions and variables.
You can declare that functions and variables are global to an
entire program, global within a "module" (source file), local to
a module, and local to a function. Regular awk only gives you
global variables, global functions, and extra function parameters,
which act as local variables.
This feature is a very nice one, making it much easier to write
libraries of awk functions without having to worry about variable names
inadvertently conflicting with those in other library functions or in
the user's main program.
can be a regular expression. This is similar to gawk and mawk;
however, the regular expression cannot be one that requires more than
one character of look-ahead. The text that matched
in the variable
(record separator match), similar to
Describing fields, instead of the field separators.
can be a regular expression that describes the
contents of the fields. Successive occurrences of text that matches
become the contents of the fields.
Controlling the implicit file processing loop.
tracks the position in
current input data file. Unlike gawk's
assigning a value to
can be used to make tawk skip over
input data files.
Fixed-length records. By assigning a value to the
you can make tawk read records in fixed-length chunks. If
not matched within
characters, then tawk returns a record
You can specify C-style hexadecimal constants (
being two rather famous ones) in tawk programs.
This helps when using the built-in bit manipulation functions
(see the next section).
Whew! That's a rather long list, but these features bring
additional power to programming in awk.
Besides extending the language, tawk provides a large number of
additional built-in functions.
Here is another "laundry list," this time of the different classes of
functions available. Each class has two or more functions associated
with it. We'll briefly describe the functionality of each class.
Extended string functions. Extensions to the standard string functions
and new string functions
allow you to match and substitute for subpatterns within patterns
(similar to gawk's
function), assign to substrings within
strings, and split a string into an array based on a pattern that
matches elements, instead of the separator. There are additional
formats, and string translation functions.
While undoubtedly some of these functions could be written as
user-defined functions, having them built in provides greater performance.
Bit manipulation functions. You can perform bitwise AND, OR, and XOR
operations on (integer) values.
These could also be written as user-defined functions, but with a loss
More I/O functions. There is a suite of functions modeled after those
(3) library. In particular, the ability to seek within
a file, and do I/O in fixed-size amounts, is quite useful.
Directory operation functions. You can make, remove, and change directories,
as well as remove and rename files.
File information functions. You can retrieve file permissions, size, and
Directory reading functions. You can get the current directory name,
as well as read a list of all the filenames in a directory.
Time functions. There are functions to retrieve the current time of day,
and format it in various ways. These functions are not quite as flexible
Execution functions. You can sleep for a specific amount of time, and
start other functions running. Tawk's
interesting because it allows you to provide values for the new
program's environment, and also indicate whether the program should or
should not run asynchronously.
This is particularly valuable on non-UNIX systems, where the command
interpreters (such as MS-DOS's
) are quite limited.
File locking. You can lock and unlock files and ranges within files.
Screen functions. You can do screen-oriented I/O. Under UNIX,
these functions are implemented on top of the
Packing and unpacking of binary data. You can specify how binary data
structures are laid out. This, together with the new I/O functions, makes
it possible to do binary I/O, something you would normally have to do in C
Access to internal state. You can get or set the value of any awk variable
through function calls.
Access to MS-DOS low-level facilities. You can use system interrupts,
and peek and poke values at memory addresses. These features are
obviously for experts only.
From this list, it becomes clear that tawk provides a nice alternative
to C and to Perl for serious programming tasks.
As an example, the
screen functions and internal state functions are used to
implement the tawk debugger in awk.
sells software called VSAwk that brings awk-style
programming into the Visual Basic environment.
VSAwk is a Visual Basic
control that works in an event driven fashion.
Like awk, VSAwk gives you startup and cleanup actions, and splits
the input record into fields,
as well as the ability to write
expressions and call the awk built-in functions.
VSAwk resembles UNIX awk mostly in its data processing model, not
Nevertheless, it's interesting to see how people apply the concepts
from awk to the environment provided by a very different language.