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 the standard input.

Variables can be assigned a value on the command line. The value assigned to a variable can be a literal, a shell variable ($ name), or a command substitution (`cmd`), but the value is available only after a line of input is read (i.e., after the BEGIN statement).

For example, to print the first three (colon-separated) fields of the password file, use -F to set the field separator to a colon:

            gawk -F: '{print $1; print $2; print $3}' /etc/passwd

Numerous examples are shown later in Section 13.3.

Options

All options exist in both traditional POSIX (one-letter) format and GNU-style (long) format. Some recognized options are:

--

Treat all subsequent text as commands or filenames, not options.

-f scriptfile, --file= scriptfile

Read gawk commands from scriptfile instead of command line.

-v var = value, --assign= var = value

Assign a value to variable var. This allows assignment before the script begins execution.

-F c, --field-separator= c

Set the field ...

Get Linux in a Nutshell, Third 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.