Man Pages
General Commands
NAME
gawk - pattern scanning and processing language
SYNOPSIS
gawk [ <FONT SIZE="-1">POSIX</FONT> or <FONT SIZE="-1">GNU</FONT> style options ]
-f program-file [
-- ] file ...
gawk [ <FONT SIZE="-1">POSIX</FONT> or <FONT SIZE="-1">GNU</FONT> style options ] [
-- ]
program-text file ...
pgawk [ <FONT SIZE="-1">POSIX</FONT> or <FONT SIZE="-1">GNU</FONT> style options ] -f program-file [ -- ] file ...
pgawk [ <FONT SIZE="-1">POSIX</FONT> or <FONT SIZE="-1">GNU</FONT> style options ] [ -- ] program-text file ...
DESCRIPTION
Gawk is the <FONT SIZE="-1">GNU</FONT> Project`s implementation of the <FONT SIZE="-1">AWK</FONT> programming language. It conforms to the definition of the language in the <FONT SIZE="-1">POSIX</FONT> 1003.2 Command Language And Utilities Standard. This version in turn is based on the description in
The AWK Programming Language, by Aho, Kernighan, and Weinberger, with the additional features found in the System V Release 4 version of <FONT SIZE="-1">UNIX</FONT>
awk.
Gawk also provides more recent Bell Laboratories
awk extensions, and a number of <FONT SIZE="-1">GNU</FONT>-specific extensions.
Pgawk is the profiling version of gawk. It is identical in every way to gawk, except that programs run more slowly, and it automatically produces an execution profile in the file awkprof.out when done. See the --profile option, below.
The command line consists of options to gawk itself, the <FONT SIZE="-1">AWK</FONT> program text (if not supplied via the -f or --file options), and values to be made available in the ARGC and ARGV pre-defined <FONT SIZE="-1">AWK</FONT> variables.
OPTION FORMAT
Gawk options may be either traditional <FONT SIZE="-1">POSIX</FONT> one letter options, or <FONT SIZE="-1">GNU</FONT> style long options. <FONT SIZE="-1">POSIX</FONT> options start with a single ``-``, while long options start with ``--``. Long options are provided for both <FONT SIZE="-1">GNU</FONT>-specific features and for <FONT SIZE="-1">POSIX</FONT>-mandated features.
Following the <FONT SIZE="-1">POSIX</FONT> standard, gawk-specific options are supplied via arguments to the -W option. Multiple -W options may be supplied Each -W option has a corresponding long option, as detailed below. Arguments to long options are either joined with the option by an = sign, with no intervening spaces, or they may be provided in the next command line argument. Long options may be abbreviated, as long as the abbreviation remains unique.
OPTIONS
Gawk accepts the following options, listed alphabetically.
-
- -F fs
-
- --field-separator fs Use fs for the input field separator (the value of the FS predefined variable).
-
- -v var=val
-
- --assign var=val Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the BEGIN block of an <FONT SIZE="-1">AWK</FONT> program.
-
- -f program-file
-
- --file program-file Read the <FONT SIZE="-1">AWK</FONT> program source from the file program-file, instead of from the first command line argument. Multiple -f (or --file) options may be used.
-
- -mf NNN
-
- -mr NNN Set various memory limits to the value NNN. The f flag sets the maximum number of fields, and the r flag sets the maximum record size. These two flags and the -m option are from the Bell Laboratories research version of <FONT SIZE="-1">UNIX</FONT> awk. They are ignored by gawk, since gawk has no pre-defined limits.
-
- -W compat
-
- -W traditional
-
- --compat
-
- --traditional Run in compatibility mode. In compatibility mode, gawk behaves identically to <FONT SIZE="-1">UNIX</FONT> awk; none of the <FONT SIZE="-1">GNU</FONT>-specific extensions are recognized. The use of --traditional is preferred over the other forms of this option. See GNU EXTENSIONS, below, for more information.
-
- -W copyleft
-
- -W copyright
-
- --copyleft
-
- --copyright Print the short version of the <FONT SIZE="-1">GNU</FONT> copyright information message on the standard output and exit successfully.
-
- -W dump-variables[=file]
-
- --dump-variables[=file] Print a sorted list of global variables, their types and final values to file. If no file is provided, gawk uses a file named awkvars.out in the current directory.
Having a list of all the global variables is a good way to look for typographical errors in your programs. You would also use this option if you have a large program with a lot of functions, and you want to be sure that your functions don`t inadvertently use global variables that you meant to be local. (This is a particularly easy mistake to make with simple variable names like i, j, and so on.)
-
- -W help
-
- -W usage
-
- --help
-
- --usage Print a relatively short summary of the available options on the standard output. (Per the GNU Coding Standards, these options cause an immediate, successful exit.)
-
- -W lint[=value]
-
- --lint[=value] Provide warnings about constructs that are dubious or non-portable to other <FONT SIZE="-1">AWK</FONT> implementations. With an optional argument of fatal, lint warnings become fatal errors. This may be drastic, but its use will certainly encourage the development of cleaner <FONT SIZE="-1">AWK</FONT> programs. With an optional argument of invalid, only warnings about things that are actually invalid are issued. (This is not fully implemented yet.)
-
- -W lint-old
-
- --lint-old Provide warnings about constructs that are not portable to the original version of Unix awk.
-
- -W gen-po
-
- --gen-po Scan and parse the <FONT SIZE="-1">AWK</FONT> program, and generate a <FONT SIZE="-1">GNU</FONT> .po format file on standard output with entries for all localizable strings in the program. The program itself is not executed. See the <FONT SIZE="-1">GNU</FONT> gettext distribution for more information on .po files.
-
- -W non-decimal-data
-
- --non-decimal-data Recognize octal and hexadecimal values in input data. Use this option with great caution!
-
- -W posix
-
- --posix This turns on compatibility mode, with the following additional restrictions:
-
- *
- x escape sequences are not recognized.
- *
- Only space and tab act as field separators when FS is set to a single space, newline does not.
- *
- You cannot continue lines after ? and :.
- *
- The synonym func for the keyword function is not recognized.
- *
- The operators ** and **= cannot be used in place of ^ and ^=.
- *
- The fflush() function is not available.
-
- -W profile[=prof_file]
-
- --profile[=prof_file] Send profiling data to prof_file. The default is awkprof.out. When run with gawk, the profile is just a ``pretty printed`` version of the program. When run with pgawk, the profile contains execution counts of each statement in the program in the left margin and function call counts for each user-defined function.
-
- -W re-interval
-
- --re-interval Enable the use of interval expressions in regular expression matching (see Regular Expressions, below). Interval expressions were not traditionally available in the <FONT SIZE="-1">AWK</FONT> language. The <FONT SIZE="-1">POSIX</FONT> standard added them, to make awk and egrep consistent with each other. However, their use is likely to break old <FONT SIZE="-1">AWK</FONT> programs, so gawk only provides them if they are requested with this option, or when --posix is specified.
-
- -W source program-text
-
- --source program-text Use program-text as <FONT SIZE="-1">AWK</FONT> program source code. This option allows the easy intermixing of library functions (used via the -f and --file options) with source code entered on the command line. It is intended primarily for medium to large <FONT SIZE="-1">AWK</FONT> programs used in shell scripts.
-
- -W version
-
- --version Print version information for this particular copy of gawk on the standard output. This is useful mainly for knowing if the current copy of gawk on your system is up to date with respect to whatever the Free Software Foundation is distributing. This is also useful when reporting bugs. (Per the GNU Coding Standards, these options cause an immediate, successful exit.)
-
- -- Signal the end of options. This is useful to allow further arguments to the <FONT SIZE="-1">AWK</FONT> program itself to start with a ``-``. This is mainly for consistency with the argument parsing convention used by most other <FONT SIZE="-1">POSIX</FONT> programs.
In compatibility mode, any other options are flagged as invalid, but are otherwise ignored. In normal operation, as long as program text has been supplied, unknown options are passed on to the <FONT SIZE="-1">AWK</FONT> program in the ARGV array for processing. This is particularly useful for running <FONT SIZE="-1">AWK</FONT> programs via the ``#!`` executable interpreter mechanism.
AWK PROGRAM EXECUTION
An <FONT SIZE="-1">AWK</FONT> program consists of a sequence of pattern-action statements and optional function definitions.
-
pattern { action statements }
function name(parameter list) { statements }
Gawk first reads the program source from the program-file(s) if specified, from arguments to --source, or from the first non-option argument on the command line. The -f and --source options may be used multiple times on the command line. Gawk reads the program text as if all the program-files and command line source texts had been concatenated together. This is useful for building libraries of <FONT SIZE="-1">AWK</FONT> functions, without having to include them in each new <FONT SIZE="-1">AWK</FONT> program that uses them. It also provides the ability to mix library functions with command line programs.
The environment variable AWKPATH specifies a search path to use when finding source files named with the -f option. If this variable does not exist, the default path is ".:/usr/local/share/awk". (The actual directory may vary, depending upon how gawk was built and installed.) If a file name given to the -f option contains a ``/`` character, no path search is performed.
Gawk executes <FONT SIZE="-1">AWK</FONT> programs in the following order. First, all variable assignments specified via the -v option are performed. Next, gawk compiles the program into an internal form. Then, gawk executes the code in the BEGIN block(s) (if any), 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 filename on the command line has the form var=val it is treated as a variable assignment. The variable var will be assigned the value val. (This happens after any BEGIN block(s) have been run.) Command line variable assignment is most useful for dynamically assigning values to the variables <FONT SIZE="-1">AWK</FONT> uses to control how input is broken into fields and records. It is also useful for controlling state if multiple passes are needed over a single data file.
If the value of a particular element of ARGV is empty (""), gawk skips over it.
For each record in the input, gawk tests to see if it matches any pattern in the <FONT SIZE="-1">AWK</FONT> program. For each pattern that the record matches, the associated action is executed. The patterns are tested in the order they occur in the program.
Finally, after all the input is exhausted, gawk executes the code in the END block(s) (if any).
VARIABLES, RECORDS AND FIELDS
<FONT SIZE="-1">AWK</FONT> variables are dynamic; they come into existence when they are first used. Their values are either floating-point numbers or strings, or both, depending upon how they are used. <FONT SIZE="-1">AWK</FONT> also has one dimensional arrays; arrays with multiple dimensions may be simulated. Several pre-defined variables are set as a program runs; these will be described as needed and summarized below.
Records
Normally, records are separated by newline characters. You can control how records are separated by assigning values to the built-in variable
RS. If
RS is any single character, that character separates records. Otherwise,
RS is a regular expression. Text in the input that matches this regular expression separates the record. However, in compatibility mode, 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, the newline character always acts as a field separator, in addition to whatever value
FS may have.
Fields
As each input record is read, gawk splits the record 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. If FS is the null string, then each individual character becomes a separate field. Otherwise, FS is expected to be a full regular expression. In the special case that FS is a single space, fields are separated by runs of spaces and/or tabs and/or newlines. (But see the discussion of --posix, below). NOTE: The value of IGNORECASE (see below) also affects how fields are split when FS is a regular expression, and how records are separated when RS is a regular expression.
If the FIELDWIDTHS variable is set to a space separated list of numbers, each field is expected to have fixed width, and gawk splits up the record using the specified widths. The value of FS is ignored. Assigning a new value to FS overrides the use of FIELDWIDTHS, and restores the default behavior.
Each field in the input record may be referenced by its position, $1, $2, and so on. $0 is the whole record. Fields need not be referenced by constants:
-
n = 5
print $n
prints the fifth field in the input record.
The variable NF is set to the total number of fields in the input record.
References to non-existent fields (i.e. fields after $NF) produce the null-string. However, assigning to a non-existent 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. References to negative numbered fields cause a fatal error. Decrementing NF causes the values of fields past the new value to be lost, and the value of $0 to be recomputed, with the fields being separated by the value of OFS.
Assigning a value to an existing field causes the whole record to be rebuilt when $0 is referenced. Similarly, assigning a value to $0 causes the record to be resplit, creating new values for the fields.
Built-in Variables
Gawk`s built-in variables are:
- ARGC
- The number of command line arguments (does not include options to gawk, or the program source).
- ARGIND
- The index in ARGV of the current file being processed.
- ARGV
- Array of command line arguments. The array is indexed from 0 to ARGC - 1. Dynamically changing the contents of ARGV can control the files used for data.
- BINMODE
- On non-POSIX systems, specifies use of ``binary`` mode for all file I/O. Numeric values of 1, 2, or 3, specify that input files, output files, or all files, respectively, should use binary I/O. String values of "r", or "w" specify that input files, or output files, respectively, should use binary I/O. String values of "rw" or "wr" specify that all files should use binary I/O. Any other string value is treated as "rw", but generates a warning message.
- CONVFMT
- The conversion format for numbers, "%.6g", by default.
- ENVIRON
- An array containing the values of the current environment. The array is indexed by the environment variables, each element being the value of that variable (e.g., ENVIRON["HOME"] might be /home/arnold). Changing this array does not affect the environment seen by programs which gawk spawns via redirection or the system() function.
- ERRNO
- If a system error occurs either doing a redirection for getline, during a read for getline, or during a close(), then ERRNO will contain a string describing the error. The value is subject to translation in non-English locales.
- FIELDWIDTHS
- A white-space separated list of fieldwidths. When set, gawk parses the input into fields of fixed width, instead of using the value of the FS variable as the field separator.
- FILENAME
- The name of the current input file. If no files are specified on the command line, the value of FILENAME is ``-``. However, FILENAME is undefined inside the BEGIN block (unless set by getline).
- FNR
- The input record number in the current input file.
- FS
- The input field separator, a space by default. See Fields, above.
- IGNORECASE
- Controls the case-sensitivity of all regular expression and string operations. If IGNORECASE has a non-zero value, then string comparisons and pattern matching in rules, field splitting with FS, record separating with RS, regular expression matching with ~ and !~, and the gensub(), gsub(), index(), match(), split(), and sub() built-in functions all ignore case when doing regular expression operations. NOTE: Array subscripting is not affected. However, the asort() and asorti() functions are affected.
Thus, if IGNORECASE is not equal to zero, /aB/ matches all of the strings "ab", "aB", "Ab", and "AB". As with all <FONT SIZE="-1">AWK</FONT> variables, the initial value of IGNORECASE is zero, so all regular expression and string operations are normally case-sensitive. Under Unix, the full ISO 8859-1 Latin-1 character set is used when ignoring case.
- LINT
- Provides dynamic control of the --lint option from within an <FONT SIZE="-1">AWK</FONT> program. When true, gawk prints lint warnings. When false, it does not. When assigned the string value "fatal", lint warnings become fatal errors, exactly like --lint=fatal. Any other true value just prints warnings.
- NF
- The number of fields in the current input record.
- NR
- The total number of input records seen so far.
- OFMT
- The output format for numbers, "%.6g", by default.
- OFS
- The output field separator, a space by default.
- ORS
- The output record separator, by default a newline.
- PROCINFO
- The elements of this array provide access to information about the running <FONT SIZE="-1">AWK</FONT> program. On some systems, there may be elements in the array, "group1" through "groupn" for some n, which is the number of supplementary groups that the process has. Use the in operator to test for these elements. The following elements are guaranteed to be available:
-
- PROCINFO["egid"]
- the value of the getegid(2) system call.
- PROCINFO["euid"]
- the value of the geteuid(2) system call.
- PROCINFO["FS"]
- "FS" if field splitting with FS is in effect, or "FIELDWIDTHS" if field splitting with FIELDWIDTHS is in effect.
- PROCINFO["gid"]
- the value of the getgid(2) system call.
- PROCINFO["pgrpid"]
- the process group ID of the current process.
- PROCINFO["pid"]
- the process ID of the current process.
- PROCINFO["ppid"]
- the parent process ID of the current process.
- PROCINFO["uid"]
- the value of the getuid(2) system call.
- RS
- The input record separator, by default a newline.
- RT
- The record terminator. Gawk sets RT to the input text that matched the character or regular expression specified by RS.
- RSTART
- The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)
- RLENGTH
- The length of the string matched by match(); -1 if no match.
- SUBSEP
- The character used to separate multiple subscripts in array elements, by default "