| iMatix home page
| Libero home page | Libero documentation
| << | < | > | >>
Libero Libero
Version 2.32

 

The Awk Language

Overview

What Is Awk?
Awk is a standard UNIX tool that comes in several flavours - 'old' awk (/bin/awk), new awk (/bin/nawk), GNU gawk (/bin/gawk). It is a small interpreted language that looks a bit like C, with a strong flavour of sed pattern matching. Awk is mostly used to manipulate text files, but is also fun as a general-purpose programming language.
Portability
GNU gawk is available on many platforms. Awk scripts are highly portable if you don't rely on (UNIX) system-dependent commands. GNU Gawk and new awk share most functions.
Pros and Cons
Pros: Awk is versatile and easy to learn; GNU gawk is freely available; Awk is an interpreted language and looks a bit like C. Cons: Awk is a simple language, not powerful enough for industrial-strength work; it's interpreted - slower than C but faster than a shell; it looks a bit like C.
How To Get It
On UNIX boxes, awk or nawk. On other systems, get GNU gawk.
Libero Supports
Libero supports new awk and gawk, but not old awk.

Working With Awk

Example of code

Code fragments:

#################   MODULE FUNCTION NAME   #################

function module_function_name  ( local_var,... ) {            # Place local variables here
    return                      # Empty module needs 'return'
}
function private_function_name (argument,...) {
    the_next_event = ok_event
    raise_exception (error_event)
}

A fuller example.

Notes

My awk programs fall between two extremes. At the one end is a 'filter', to process text according to patterns in each line. This is excellent for smallish problems. As the problem grows, I need to read other files 'on the side', and the logic of my awk program grows until a large part of it is sitting in the BEGIN or END blocks. Eventually I find that I am perhaps reading and writing several files, so I take the plunge and rewrite the program to handle all files with explicit 'getline' calls. The awk program is no longer a filter, but a stand-alone program that resembles a classic procedural program. Of course at this point my Libero hindbrain wakes up and starts muttering 'an' whaddabout Libero, eh?'.

When I wrote the awk schema, I started with the Visual Basic schema. Bizzarely enough this language resembles awk most in terms of structure and possibilities. I've started thinking of awk as a kind of 'Basic for UNIX'.

Awk presents some of the same difficulties as Visual Basic. There is no 'include' statement to take an external file as part of the program. The UNIX shells have the same problem.

<GRIPE>Since awk is tokenised, then executed, messieurs A, W, and K had no real excuse for omitting an 'include' facility.</GRIPE>

I really don't want to look at the chunk of code that Libero generates, so I always try to find a way to separate this from the hand-written code I am working on. If the language provides an 'include' statement, I place the generated code in an include file. This works great in serious compiled languages which all have something like this. You change the dialog, regenerate the code, and recompile. At any time you can change the program and recompile without having to run Libero again.

For the awk schema I used another technique: you work in a separate 'module' file (myprog.mod). Libero combines the module file with the generated program to create a final program, which you don't change. This effectively hides the generated code, but introduces an extra step: each time you change the .mod file you must re-run Libero to create the final script. In effect, Libero acting as a preprocessor in place of the missing 'include' statement.

Notice the slightly unusual format of a generated module header:

function module_function_name  ( local_var,... ) {
    ...
}

I break the function header over two lines so that you can add local variables easily if you need to. Libero recognises the first line as a module header, and does not look at the next line with the local variables. The unusual format also distinguishes module functions from private functions, so ensures that the -idle option works correctly.


| << | < | > | >>
| Libero's Languages | The Awk Language | The C Language | The C++ Language | The COBOL Language | The GNU Borne-Again Shell Language | The Java Language | The Microsoft Setup Basic Language | The MS Visual Basic Language | The Perl Language | The Rexx Language | The UNIX C Shell Language | The UNIX Bourne Shell Language | The UNIX Korn Shell Language | The 80x86 Assembler Language
iMatix
Copyright © 1996-97 iMatix