Command-Line Syntax
Patterns and Procedures
gawk System Variables
PROCINFO Array
Operators
Variable and Array Assignments
Group Listing of gawk Commands
Alphabetical Summary of Commands
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).
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, 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.2.
All options exist in both traditional POSIX (one-letter) format and GNU-style (long) format. Some recognized options are:
Copyright © 2003 O'Reilly & Associates. All rights reserved.