O'Reilly logo

Linux in a Nutshell, Fourth Edition by Aaron Weber, Stephen Figgins, Ellen Siever

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Patterns and Procedures

gawk scripts consist of patterns and procedures:

            pattern {procedure}

Both are optional. If pattern is missing, { procedure } is applied to all records. If { procedure } is missing, the matched record is printed. By default, each line of input is a record, but you can specify a different record separator through the RS variable.

Patterns

A pattern can be any of the following:

/regular expression/
relational expression
               pattern-matching expression
               pattern,pattern
               BEGIN
               END

Some rules regarding patterns include:

  • Expressions can be composed of quoted strings, numbers, operators, functions, defined variables, or any of the predefined variables described later under “gawk System Variables.”

  • Regular expressions use the extended set of metacharacters and are described in Chapter 9.

  • In addition, ^ and $ can be used to refer to the beginning and end of a field, respectively, rather than the beginning and end of a record.

  • Relational expressions use the relational operators listed under “Operators” later in this chapter. Comparisons can be either string or numeric. For example, $2 > $1 selects lines for which the second field is greater than the first.

  • Pattern-matching expressions use the operators ~ (match) and !~ (don’t match). See “Operators” later in this chapter.

  • The BEGIN pattern lets you specify procedures that take place before the first input record is processed. (Generally, you set global variables here.)

  • The END pattern lets you specify procedures that ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required