Go to the previous, next section.
gawk
Summary
This appendix provides a brief summary of the gawk
command line and the
awk
language. It is designed to serve as "quick reference." It is
therefore terse, but complete.
The command line consists of options to gawk
itself, the
awk
program text (if not supplied via the `-f' option), and
values to be made available in the ARGC
and ARGV
predefined awk
variables:
awk [POSIX or GNU style options] -f source-file [--
] file ... awk [POSIX or GNU style options] [--
] 'program' file ...
The options that gawk
accepts are:
-F fs
--field-separator=fs
FS
predefined variable).
-f program-file
--file=program-file
awk
program source from the file program-file, instead
of from the first command line argument.
-v var=val
--assign=var=val
-W compat
--compat
gawk
extensions are turned
off.
-W copyleft
-W copyright
--copyleft
--copyright
gawk
.
-W help
-W usage
--help
--usage
-W lint
--lint
awk
constructs.
-W posix
--posix
gawk
extensions
are turned off and additional restrictions apply.
-W source=program-text
--source=program-text
awk
program source code. This option allows
mixing command line source code with source code from files, and is
particularly useful for mixing command line programs with library functions.
-W version
--version
gawk
on the error
output. This option may disappear in a future version of gawk
.
--
awk
program itself to start with a `-'. This is mainly for
consistency with the argument parsing conventions of POSIX.
Any other options are flagged as invalid, but are otherwise ignored.
See section Invoking awk
, for more details.
An awk
program consists of a sequence of pattern-action statements
and optional function definitions.
pattern { action statements } function name(parameter list) { action statements }
gawk
first reads the program source from the
program-file(s) if specified, or from the first non-option
argument on the command line. The `-f' option may be used multiple
times on the command line. gawk
reads the program text from all
the program-file files, effectively concatenating them in the
order they are specified. This is useful for building libraries of
awk
functions, without having to include them in each new
awk
program that uses them. To use a library function in a file
from a program typed in on the command line, specify `-f /dev/tty';
then type your program, and end it with a Control-d.
See section Invoking awk
.
The environment variable AWKPATH
specifies a search path to use
when finding source files named with the `-f' option. The default
path, which is
`.:/usr/lib/awk:/usr/local/lib/awk' is used if AWKPATH
is not set.
If a file name given to the `-f' option contains a `/' character,
no path search is performed.
See section The AWKPATH
Environment Variable,
for a full description of the AWKPATH
environment variable.
gawk
compiles the program into an internal form, and then proceeds to
read each file named in the ARGV
array. If there are no files named
on the command line, gawk
reads the standard input.
If a "file" named on the command line has the form `var=val', it is treated as a variable assignment: the variable var is assigned the value val. If any of the files have a value that is the null string, that element in the list is skipped.
For each line in the input, gawk
tests to see if it matches any
pattern in the awk
program. For each pattern that the line
matches, the associated action is executed.
awk
variables are dynamic; they come into existence when they are
first used. Their values are either floating-point numbers or strings.
awk
also has one-dimension arrays; multiple-dimensional arrays
may be simulated. There are several predefined variables that
awk
sets as a program runs; these are summarized below.
As each input line is read, gawk
splits the line into
fields, using the value of the FS
variable as the field
separator. If FS
is a single character, fields are separated by
that character. Otherwise, FS
is expected to be a full regular
expression. In the special case that FS
is a single blank,
fields are separated by runs of blanks and/or tabs. Note that the value
of IGNORECASE
(see section Case-sensitivity in Matching)
also affects how fields are split when FS
is a regular expression.
Each field in the input line may be referenced by its position, $1
,
$2
, and so on. $0
is the whole line. The value of a field may
be assigned to as well. Field numbers need not be constants:
n = 5 print $n
prints the fifth field in the input line. The variable NF
is set to
the total number of fields in the input line.
References to nonexistent fields (i.e., fields after $NF
) return
the null-string. However, assigning to a nonexistent field (e.g.,
$(NF+2) = 5
) increases the value of NF
, creates any
intervening fields with the null string as their value, and causes the
value of $0
to be recomputed, with the fields being separated by
the value of OFS
.
See section Reading Input Files, for a full description of the
way awk
defines and uses fields.
awk
's built-in variables are:
ARGC
awk
program itself).
ARGIND
ARGV
of the current file being processed.
It is always true that `FILENAME == ARGV[ARGIND]'.
ARGV
ARGC
- 1. Dynamically changing the contents of ARGV
can control the files used for data.
CONVFMT
FIELDWIDTHS
ENVIRON
HOME
would be in
ENVIRON["HOME"]
. Its value might be `/u/close'.
Changing this array does not affect the environment seen by programs
which gawk
spawns via redirection or the system
function.
(This may change in a future version of gawk
.)
Some operating systems do not have environment variables.
The array ENVIRON
is empty when running on these systems.
ERRNO
getline
or close
.
FILENAME
FILENAME
is `-'.
FNR
FS
IGNORECASE
IGNORECASE
has a nonzero value, then pattern matching in rules,
field splitting with FS
, regular expression matching with
`~' and `!~', and the gsub
, index
, match
,
split
and sub
predefined functions all ignore case
when doing regular expression operations.
NF
NR
OFMT
print
statement,
"%.6g"
by default.
OFS
ORS
RS
RS
is exceptional
in that only the first character of its string value is used for separating
records. If RS
is set to the null string, then records are separated by
blank lines. When RS
is set to the null string, then the newline
character always acts as a field separator, in addition to whatever value
FS
may have.
RSTART
match
; 0 if no match.
RLENGTH
match
; -1 if no match.
SUBSEP
"\034"
.
See section Built-in Variables, for more information.
Arrays are subscripted with an expression between square brackets (`[' and `]'). Array subscripts are always strings; numbers are converted to strings as necessary, following the standard conversion rules (see section Conversion of Strings and Numbers).
If you use multiple expressions separated by commas inside the square
brackets, then the array subscript is a string consisting of the
concatenation of the individual subscript values, converted to strings,
separated by the subscript separator (the value of SUBSEP
).
The special operator in
may be used in an if
or
while
statement to see if an array has an index consisting of a
particular value.
if (val in array) print array[val]
If the array has multiple subscripts, use (i, j, ...) in array
to test for existence of an element.
The in
construct may also be used in a for
loop to iterate
over all the elements of an array.
See section Scanning all Elements of an Array.
An element may be deleted from an array using the delete
statement.
See section Arrays in awk
, for more detailed information.
The value of an awk
expression is always either a number
or a string.
Certain contexts (such as arithmetic operators) require numeric values. They convert strings to numbers by interpreting the text of the string as a numeral. If the string does not look like a numeral, it converts to 0.
Certain contexts (such as concatenation) require string values.
They convert numbers to strings by effectively printing them
with sprintf
.
See section Conversion of Strings and Numbers, for the details.
To force conversion of a string value to a number, simply add 0 to it. If the value you start with is already a number, this does not change it.
To force conversion of a numeric value to a string, concatenate it with the null string.
The awk
language defines comparisons as being done numerically if
both operands are numeric, or if one is numeric and the other is a numeric
string. Otherwise one or both operands are converted to strings and a
string comparison is performed.
Uninitialized variables have the string value ""
(the null, or
empty, string). In contexts where a number is required, this is
equivalent to 0.
See section Variables, for more information on variable naming and initialization; see section Conversion of Strings and Numbers, for more information on how variable values are interpreted.
An awk
program is mostly composed of rules, each consisting of a
pattern followed by an action. The action is enclosed in `{' and
`}'. Either the pattern may be missing, or the action may be
missing, but, of course, not both. If the pattern is missing, the
action is executed for every single line of input. A missing action is
equivalent to this action,
{ print }
which prints the entire line.
Comments begin with the `#' character, and continue until the end of the
line. Blank lines may be used to separate statements. Normally, a statement
ends with a newline, however, this is not the case for lines ending in a
`,', `{', `?', `:', `&&', or `||'. Lines
ending in do
or else
also have their statements automatically
continued on the following line. In other cases, a line can be continued by
ending it with a `\', in which case the newline is ignored.
Multiple statements may be put on one line by separating them with a `;'. This applies to both the statements within the action part of a rule (the usual case), and to the rule statements.
See section Comments in awk
Programs, for information on
awk
's commenting convention;
see section awk
Statements versus Lines, for a
description of the line continuation mechanism in awk
.
awk
patterns may be one of the following:
/regular expression/ relational expression pattern && pattern pattern || pattern pattern ? pattern : pattern (pattern) ! pattern pattern1, pattern2 BEGIN END
BEGIN
and END
are two special kinds of patterns that are not
tested against the input. The action parts of all BEGIN
rules are
merged as if all the statements had been written in a single BEGIN
rule. They are executed before any of the input is read. Similarly, all the
END
rules are merged, and executed when all the input is exhausted (or
when an exit
statement is executed). BEGIN
and END
patterns cannot be combined with other patterns in pattern expressions.
BEGIN
and END
rules cannot have missing action parts.
For `/regular-expression/' patterns, the associated statement is
executed for each input line that matches the regular expression. Regular
expressions are extensions of those in egrep
, and are summarized below.
A relational expression may use any of the operators defined below in the section on actions. These generally test whether certain fields match certain regular expressions.
The `&&', `||', and `!' operators are logical "and," logical "or," and logical "not," respectively, as in C. They do short-circuit evaluation, also as in C, and are used for combining more primitive pattern expressions. As in most languages, parentheses may be used to change the order of evaluation.
The `?:' operator is like the same operator in C. If the first pattern matches, then the second pattern is matched against the input record; otherwise, the third is matched. Only one of the second and third patterns is matched.
The `pattern1, pattern2' form of a pattern is called a range pattern. It matches all input lines starting with a line that matches pattern1, and continuing until a line that matches pattern2, inclusive. A range pattern cannot be used as an operand to any of the pattern operators.
See section Patterns, for a full description of the pattern part of awk
rules.
Regular expressions are the extended kind found in egrep
.
They are composed of characters as follows:
c
\c
.
^
$
[abc...]
[^abc...]
r1|r2
r1r2
r+
r*
r?
(r)
See section Regular Expressions as Patterns, for a more detailed explanation of regular expressions.
The escape sequences allowed in string constants are also valid in regular expressions (see section Constant Expressions).
Action statements are enclosed in braces, `{' and `}'. Action statements consist of the usual assignment, conditional, and looping statements found in most languages. The operators, control statements, and input/output statements available are patterned after those in C.
The operators in awk
, in order of increasing precedence, are:
= += -= *= /= %= ^=
var=value
)
and operator assignment (the other forms) are supported.
?:
expr1 ?
expr2 : expr3
. If expr1 is true, the value of the
expression is expr2; otherwise it is expr3. Only one of
expr2 and expr3 is evaluated.
||
&&
~ !~
< <= > >= != ==
blank
+ -
* / %
+ - !
^
++ --
$
See section Expressions as Action Statements, for a full description of all the operators listed above. See section Examining Fields, for a description of the field reference operator.
The control statements are as follows:
if (condition) statement [ else statement ] while (condition) statement do statement while (condition) for (expr1; expr2; expr3) statement for (var in array) statement break continue delete array[index] exit [ expression ] { statements }
See section Control Statements in Actions, for a full description of all the control statements listed above.
The input/output statements are as follows:
getline
$0
from next input record; set NF
, NR
, FNR
.
getline <file
$0
from next record of file; set NF
.
getline var
NF
, FNR
.
getline var <file
next
awk
program.
If the end of the input data is reached, the END
rule(s), if any,
are executed.
next file
FILENAME
is updated, FNR
is set to 1,
and processing starts over with the first pattern in the awk
program.
If the end of the input data is reached, the END
rule(s), if any,
are executed.
print
print expr-list
print expr-list > file
printf fmt, expr-list
printf fmt, expr-list > file
Other input/output redirections are also allowed. For print
and
printf
, `>> file' appends output to the file,
and `| command' writes on a pipe. In a similar fashion,
`command | getline' pipes input into getline
.
getline
returns 0 on end of file, and -1 on an error.
See section Explicit Input with getline
, for a full description
of the getline
statement.
See section Printing Output, for a full description of print
and
printf
. Finally, see section The next
Statement,
for a description of how the next
statement works.
printf
Summary
The awk
printf
statement and sprintf
function
accept the following conversion specification formats:
%c
%d
%i
%e
%f
-
]ddd.dddddd
.
%g
%o
%s
%x
%X
%%
There are optional, additional parameters that may lie between the `%' and the control letter:
-
width
.prec
Either or both of the width and prec values may be specified as `*'. In that case, the particular value is taken from the argument list.
See section Using printf
Statements for Fancier Printing, for
examples and for a more detailed description.
When doing I/O redirection from either print
or printf
into a
file, or via getline
from a file, gawk
recognizes certain special
file names internally. These file names allow access to open file descriptors
inherited from gawk
's parent process (usually the shell). The
file names are:
In addition the following files provide process related information
about the running gawk
program.
$1
getuid
system call.
$2
geteuid
system call.
$3
getgid
system call.
$4
getegid
system call.
If there are any additional fields, they are the group IDs returned by
getgroups
system call.
(Multiple groups may not be supported on all systems.)
These file names may also be used on the command line to name data files. These file names are only recognized internally if you do not actually have files by these names on your system.
See section Standard I/O Streams, for a longer description that provides the motivation for this feature.
awk
has the following predefined arithmetic functions:
atan2(y, x)
cos(expr)
exp(expr)
int(expr)
log(expr)
rand()
sin(expr)
sqrt(expr)
srand(expr)
awk
has the following predefined string functions:
gsub(r, s, t)
$0
.
index(s, t)
length(s)
$0
is returned if no argument is supplied.
match(s, r)
RSTART
and RLENGTH
.
split(s, a, r)
FS
is used instead.
sprintf(fmt, expr-list)
sub(r, s, t)
gsub
, but only the first matching substring is
replaced.
substr(s, i, n)
tolower(str)
toupper(str)
system(cmd-line)
The following two functions are available for getting the current time of day, and for formatting time stamps.
systime()
strftime(format, timestamp)
strftime
accepts.
See section Built-in Functions, for a description of all of
awk
's built-in functions.
String constants in awk
are sequences of characters enclosed
between double quotes ("
). Within strings, certain escape sequences
are recognized, as in C. These are:
\\
\a
\b
\f
\n
\r
\t
\v
\xhex digits
"\x1B"
is a
string containing the ASCII ESC (escape) character. (The `\x'
escape sequence is not in POSIX awk
.)
\ddd
"\033"
is also a string containing the ASCII ESC
(escape) character.
\c
The escape sequences may also be used inside constant regular expressions
(e.g., the regexp /[ \t\f\n\r\v]/
matches whitespace
characters).
See section Constant Expressions.
Functions in awk
are defined as follows:
function name(parameter list) { statements }
Actual parameters supplied in the function call are used to instantiate the formal parameters declared in the function. Arrays are passed by reference, other variables are passed by value.
If there are fewer arguments passed than there are names in parameter-list, the extra names are given the null string as value. Extra names have the effect of local variables.
The open-parenthesis in a function call of a user-defined function must immediately follow the function name, without any intervening white space. This is to avoid a syntactic ambiguity with the concatenation operator.
The word func
may be used in place of function
(but not in
POSIX awk
).
Use the return
statement to return a value from a function.
See section User-defined Functions, for a more complete description.
There are two features of historical awk
implementations that
gawk
supports. First, it is possible to call the length
built-in function not only with no arguments, but even without parentheses!
a = length
is the same as either of
a = length() a = length($0)
This feature is marked as "deprecated" in the POSIX standard, and
gawk
will issue a warning about its use if `-W lint' is
specified on the command line.
The other feature is the use of the continue
statement outside the
body of a while
, for
, or do
loop. Traditional
awk
implementations have treated such usage as equivalent to the
next
statement. gawk
will support this usage if `-W posix'
has not been specified.
Go to the previous, next section.