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 "Patterns and Procedures".

Options

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