Main index

Introducing UNIX and Linux


Awk

Overview
What is 'awk'?
Invoking 'awk'
Naming the fields
Formatted output
      Operators used by Awk
Patterns
Variables
      Accessing Values
      Special variables
Arguments to 'awk' scripts
Arrays
Field and record separators
Functions
      List of Awk functions
Summary
Exercises

What is 'awk'?

If you are familiar with high-level programming languages, you will recognise the need for such constructs as loops, variables, conditional statements, input and output facilities, and a library of predefined procedures. In other words, a rich syntax allowing many complex tasks to be performed easily and efficiently by an experienced programmer. The shell is a high-level language, although its features are tailored to a very special task, namely managing UNIX processes, and many high-level constructs are not present in the shell.

If you need to write a program that is not clearly suited to writing in the shell, you would normally choose a language such as C or Pascal in which to write it. There is a grey area, though, where the application does seem to fit in well with the ideas and methods underlying shell programming, yet the power of a full high-level language would be advantageous. A typical situation would be where the contents of a file (or output from a pipe) contains complex (numerical or textual) data, organised into records (e.g. lines) and fields (e.g. columns), and calculations are required to be performed on the data.

To illustrate this, consider data which has as its columns a person's name, followed by numbers representing that person's marks in a number of examinations. You need to process that data to determine each person's overall mark, possibly converting it to a grade, and to calculate overall statistical data for the individual examinations. Perhaps you would like a graphical display of the mark distribution. These tasks could be performed with the shell (with some difficulty), but they can be programmed naturally using Awk.

Mention has been made before of the language C - if you can program in C then Awk will look familiar to you, and vice versa. There are features of Awk, such as pattern matching and associative arrays, which are not available with C, and vice versa. If you write a program in C, then it will probably execute faster than an equivalent program in Awk. You should therefore bear in mind that significant differences do exist, and take care not to confuse the two. There is insufficient space in this book to examine Awk in great detail, so we shall concentrate on those features of Awk which complement the other shell utilities.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck