Go to the previous, next section.

Overview of Actions

An awk program or script consists of a series of rules and function definitions, interspersed. (Functions are described later. See section User-defined Functions.)

A rule contains a pattern and an action, either of which may be omitted. The purpose of the action is to tell awk what to do once a match for the pattern is found. Thus, the entire program looks somewhat like this:

[pattern] [{ action }]
[pattern] [{ action }]
...
function name (args) { ... }
...

An action consists of one or more awk statements, enclosed in curly braces (`{' and `}'). Each statement specifies one thing to be done. The statements are separated by newlines or semicolons.

The curly braces around an action must be used even if the action contains only one statement, or even if it contains no statements at all. However, if you omit the action entirely, omit the curly braces as well. (An omitted action is equivalent to `{ print $0 }'.)

Here are the kinds of statements supported in awk:

The next two chapters cover in detail expressions and control statements, respectively. We go on to treat arrays and built-in functions, both of which are used in expressions. Then we proceed to discuss how to define your own functions.

Go to the previous, next section.