Chapter 13. The gawk Scripting Language

gawk is the GNU version of awk , a powerful pattern-matching program for processing text files that may be composed of fixed- or variable-length records separated by some delineator (by default, a newline character). gawk may be used from the command line or in gawk scripts. You should normally be able to invoke this utility using either awk or gawk on the shell command line.

With gawk, you can:

  • Conveniently process a text file as though it were made up of records and fields in a textual database.

  • Use variables to change those records and fields.

  • Execute shell commands from a script.

  • Perform arithmetic and string operations.

  • Use programming constructs such as loops and conditionals.

  • Define your own functions.

  • Process the result of shell commands.

  • Produce formatted reports.

For more information on gawk, see sed & awk (O’Reilly) or Effective gawk Programming (O’Reilly).

Command-Line Syntax

gawk ’s syntax has two forms:

            gawk [options] 'script' var=value file(s)
            gawk [options] -f 
            scriptfile var=value file(s)

You can specify a script directly on the command line, or you can store a script in a scriptfile and specify it with -f. Multiple -f options are allowed; awk concatenates the files. This feature is useful for including libraries.

gawk operates on one or more input files. If none are specified (or if - is specified), gawk reads from standard input.

Variables can be assigned a value on the command line. The value assigned to a variable can be a literal, ...

Get Linux in a Nutshell, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.