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

Invoking 'awk'

Just as with Grep and Sed, simple use of Awk involves a script containing the commands that Awk uses. This script can either be a string that is an argument to awk, or can be contained in a file named by option -f.

The data which the Awk script will process is either piped from standard input, or is contained in one or more files given as arguments to the command. The data is divided into records, each of which is subdivided into fields. Unless otherwise stated, each record is a single line of the data, and fields will be separated by whitespace. For instance, to represent students with their marks, a dataset might look like:

Cringle Chris 14 75 33
Smith Sam 56 58 45
Jones Jo 9 63 51

This data contains three records (lines), each record containing five fields (columns of text). An Awk script consists of a sequence of pairs

pattern { action } 

where either the pattern or the action can be omitted. The data is read, record by record, and each record that matches a pattern in the script causes the corresponding action to be performed. If the pattern is omitted the action is performed on every single line of input data. For the rest of this chapter we shall concentrate on the format of the script, and assume that data is piped from standard input. The simplest Awk script is

{}

which will do precisely nothing for each line of data - the effect is indistinguishable from cat >/dev/null. It can be invoked either by creating a file (say awkfile) containing a single line, namely the pair of braces, and

awk -f awkfile

or by

awk ' '

(not forgetting the conventions for single and double quotes) where the script is a string following awk. For the rest of this chapter, the word script will refer to an Awk script (not a shell script), unless otherwise stated.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck