GAWK(1)
๐ gawk(1) โ the GAWK(1) manual ๐
okay bestie, gather around ๐ซถ this is the gawk man page, and it's literally the cutest pattern scanning and processing language ever โจ
โจ NAME โจ
gawk - pattern scanning and processing language ๐ฆ
โจ SYNOPSIS โจ
gawk [ POSIX or GNU style options ] -f program-file [ -- ] file ...
gawk [ POSIX or GNU style options ] [ -- ] program-text file ...
๐ README FIRST ๐
this page is a courtesy, not the full story ๐
the One Source Of Truth for gawk is the Texinfo manual, which you can read online in several formats at https://www.gnu.org/software/gawk/manual. it might also be installed in the Info subsystem on your machine, so you can reach it through the info(1) command ๐
if this man page ever contradicts the Texinfo manual, the manual wins. no debate ๐ โโ๏ธ
๐ธ DESCRIPTION ๐ธ
okay so gawk is the GNU Project's version of the AWK programming language ๐ง it follows the definition of the language in the POSIX 1003.1 standard. this version is based on the description in The AWK Programming Language, by Aho, Kernighan, and Weinberger. gawk also gets the extra features from the current version of Brian Kernighan's awk, plus a bunch of GNU-specific extensions. so iconic ๐
the command line is made of options for gawk itself, the AWK program text (unless you handed it over with -f or --include), and the values you want to pass in through the pre-defined AWK variables ARGC and ARGV โจ
๐ง PREFACE ๐ง
this page is intentionally as terse as possible ๐ the full details live in GAWK: Effective AWK Programming, so go there for the whole story on any feature. wherever it's possible, you'll also find links to the online version of the manual ๐
๐ฆ OPTION FORMAT ๐ฆ
gawk options can be either the traditional POSIX-style single-letter ones, or GNU-style long options ๐
POSIX options start with a single -, while long options start with --. long options exist for both GNU-specific features and POSIX-mandated ones, so nobody gets left out ๐ซถ
gawk-specific options are usually written in long-option form. when you give an argument to a long option, you can either join it to the option with an = sign (no spaces in between), or put it in the next command line argument. you can also abbreviate long options, as long as the abbreviation is still unique ๐
every long option also has a matching short option, so you can use that functionality from inside #! executable scripts ๐ฅ
๐ OPTIONS ๐
okay this is the main event ๐ gawk takes the following options. the standard ones come first, and then the ones for gawk extensions, sorted alphabetically by short option โจ
-f program-file,--file program-fileโ read the AWK program source from the file program-file, instead of grabbing it from the first command line argument. you can use-fmore than once ๐ files read with-fare treated as if they begin with an implicit@namespace "awk"statement.-F fs,--field-separator fsโ uses fs as the input field separator, which is the value of theFSpredefined variable ๐-v var=val,--assign var=valโ assigns the value val to the variable var before the program starts running. these variable values are ready and waiting in theBEGINrule of an AWK program ๐ง-b,--characters-as-bytesโ treats all input data as single-byte characters ๐ค the--posixoption overrides this one, so keep that in mind ๐-c,--traditionalโ runs in compatibility mode. in compatibility mode, gawk acts exactly like Brian Kernighan's awk, and none of the GNU-specific extensions get recognized ๐-C,--copyrightโ prints the short version of the GNU copyright information message to standard output, then exits successfully ๐-d[file],--dump-variables[=file]โ prints a sorted list of global variables, their types, and their final values to file. the default file isawkvars.outin the current directory ๐-D[file],--debug[=file]โ turns on debugging for AWK programs ๐ by default the debugger reads commands interactively from the keyboard (standard input). the optional file argument points to a file full of debugger commands that run non-interactively. in this mode, gawk loads the AWK source code and then prompts you for debugging commands. gawk can only debug AWK program source handed over with-fand--include. the debugger is documented in GAWK: Effective AWK Programming, which you can find at https://www.gnu.org/software/gawk/manual/html_node/Debugger.html#Debugger ๐-e program-text,--source program-textโ uses program-text as the AWK program source code. every argument you pass with-eis treated as if it begins with an implicit@namespace "awk"statement ๐ง-E file,--exec fileโ works like-f, except this option gets processed last ๐ฆ use it with#!scripts, especially for CGI applications, so nobody sneaks options or source code (!) into the command line from a URL. this option also turns off command-line variable assignments ๐-g,--gen-potโ scans and parses the AWK program, then writes a GNU .pot (Portable Object Template) format file to standard output with entries for every localizable string in the program. the program itself never actually runs ๐ธ-h,--helpโ prints a relatively short summary of the available options to standard output. per the GNU Coding Standards, these options make gawk exit immediately and successfully ๐-i include-file,--include include-fileโ loads an awk source library ๐ it searches for the library using theAWKPATHenvironment variable. if that first search fails, it tries again after adding the.awksuffix. the file only gets loaded once (duplicates are eliminated), and the code does not count as the main program source. files read with--includeare treated as if they begin with an implicit@namespace "awk"statement ๐ซถ-I,--traceโ prints the internal byte code names as they run while the program executes ๐ the trace goes to standard error, and every "op code" shows up with a+sign in front of it โจ-k,--csvโ turns on CSV special processing. check out Comma Separated Values, below, for more detail ๐-l lib,--load libโ loads a gawk extension from the shared library lib. it searches for the library using theAWKLIBPATHenvironment variable. if the first search fails, it tries again after adding the default shared library suffix for your platform. the library's initialization routine is expected to be nameddl_load()๐งฉ-L [value],--lint[=value]โ gives you warnings about constructs that are dubious or non-portable to other AWK implementations ๐ต๏ธโโ๏ธ for the list of possible values for value, see https://www.gnu.org/software/gawk/manual/html_node/Options.html#Options-M,--bignumโ forces arbitrary precision arithmetic on numbers ๐งฎ this option does nothing if gawk wasn't compiled to use the GNU MPFR and GMP libraries. (in that case, gawk issues a warning.)
NOTE: this feature is on parole ๐ฅบ the primary gawk maintainer is no longer supporting it, although there's one member of the development team who still is. if that changes, the feature will be removed from gawk.
-n,--non-decimal-dataโ recognizes octal and hexadecimal values in input data. use this option with great caution! ๐-N,--use-lc-numericโ forces gawk to use the locale's decimal point character when it parses input data ๐-o[file],--pretty-print[=file]โ writes a pretty printed version of the program to file. the default file isawkprof.outin the current directory. this option implies--no-optimize๐-O,--optimizeโ turns on gawk's default optimizations for the internal representation of the program โก this option is on by default, so you're already set ๐-p[prof-file],--profile[=prof-file]โ starts a profiling session and sends the profiling data to prof-file. the default isawkprof.outin the current directory. the profile holds execution counts for each statement in the program, shown in the left margin, plus function call counts for each user-defined function. gawk runs more slowly in this mode ๐ข this option implies--no-optimize๐-P,--posixโ turns on compatibility mode and disables a bunch of common extensions ๐ค-r,--re-intervalโ enables interval expressions in regular expression matching. interval expressions are already on by default, but this option is still around for backwards compatibility ๐ฐ๏ธ-s,--no-optimizeโ turns off gawk's default optimizations for the internal representation of the program ๐ โโ๏ธ-S,--sandboxโ runs gawk in sandbox mode ๐๏ธ this disables thesystem()function, input redirection withgetline, output redirection withprintandprintf, and loading dynamic extensions. command execution through pipelines is also disabled ๐-t,--lint-oldโ gives you warnings about constructs that aren't portable to the original version of UNIX awk ๐๏ธ-V,--versionโ prints version information for this particular copy of gawk to standard output. it's super useful when you're reporting bugs ๐ per the GNU Coding Standards, these options make gawk exit immediately and successfully โจ--โ signals the end of options. this lets further arguments meant for the AWK program itself start with a-๐ซถ
in compatibility mode, any other options get flagged as invalid, but they're otherwise ignored ๐ in normal operation, as long as program text has been supplied, unknown options get passed on to the AWK program through the ARGV array so it can handle them itself ๐ง
for POSIX compatibility, you can use the -W option, followed by the name of a long option ๐
โจ AWK PROGRAM EXECUTION โจ
okay so an AWK program is basically a sequence of optional directives, pattern-action statements, and optional function definitions ๐ the shapes look like this:
@include "filename"
@load "filename"
@namespace "name"
pattern { action statements }
function name(parameter list) { statements }
gawk first grabs the program source from the program-file(s) if you gave any, from the arguments to --source, or from the first non-option argument on the command line ๐ you can use -f and --source multiple times on the command line, and gawk reads the program text as if all the program-files and command line source texts were stuck together into one big happy string ๐ซถ
lines starting with @include let you pull other source files into your program ๐ง it's the same as using the --include option.
lines starting with @load let you load extension functions into your program โจ same deal, it's equivalent to the --load option.
the environment variable AWKPATH is the search path gawk uses when hunting down source files named with -f and --include ๐บ๏ธ if that variable doesn't exist, the default path is ".:/usr/local/share/awk". (the actual directory may vary depending on how gawk was built and installed, so don't panic if yours looks different ๐) also, if a file name given to -f contains a โ/โ character, no path search happens at all, gawk just goes straight to it.
the environment variable AWKLIBPATH is the search path for source files named with --load ๐ if it doesn't exist, the default path is "/usr/local/lib/gawk". (again, the actual directory may vary based on how gawk was built and installed ๐)
here's the order gawk runs an AWK program, bestie, so pay attention ๐
- first, all the variable assignments given with the
-voption get done ๐ธ - next, gawk compiles the program into an internal form ๐ง
- then gawk runs the code in the BEGIN rule(s), if there are any, and then starts reading each file named in the ARGV array (up to
ARGV[ARGC-1]) ๐ - if there are no files named on the command line, gawk reads the standard input instead ๐ค
if a filename on the command line looks like var=val, it gets treated as a variable assignment, meaning the variable var gets set to the value val ๐
this happens after any BEGIN rule(s) have run, so keep that in mind.
if the value of a particular element of ARGV is empty (""), gawk just skips right over it ๐
for each input file, if a BEGINFILE rule exists, gawk runs its code before it processes the contents of that file ๐ and similarly, gawk runs the code in ENDFILE rules after it's done processing the file.
for each record in the input, gawk checks whether it matches any pattern in the AWK program ๐ every pattern the record matches gets its associated action run. patterns get tested in the order they show up in the program, so order matters, periodt.
finally, once all the input is used up, gawk runs the code in the END rule(s), if any ๐
๐ Command Line Directories
okay so according to POSIX, files named on the awk command line are supposed to be text files ๐ if they're not, the behavior is โundefinedโ, which is the programming equivalent of "who knows what's gonna happen" ๐ most versions of awk treat a directory on the command line as a fatal error, so yikes.
gawk is way nicer about it ๐ฆ for gawk, a directory on the command line just produces a warning, and then gets skipped. BUT if either of the --posix or --traditional options is given, gawk goes back to treating directories on the command line as a fatal error, so be careful if you're going for strict vibes ๐ญ
๐ธ VARIABLES, RECORDS AND FIELDS ๐ธ
AWK variables are dynamic, which means they come into existence the moment they're first used ๐ง their values are either floating-point numbers or strings, or both, depending on how you use them. gawk also lets variables have regular-expression type, which is so iconic ๐ AWK has one-dimensional arrays, and you can simulate multiple dimensions, but gawk gives you true arrays of arrays too ๐ several pre-defined variables get set while a program runs. they're described as needed and summarized below โจ
๐ Records
normally, records are separated by newline characters ๐ you can control how records get separated by assigning values to the built-in variable RS. for all the details, check out https://www.gnu.org/software/gawk/manual/html_node/Records.html ๐ซถ
๐งฉ Fields
as each input record is read, gawk splits it into fields, using the value of the FS variable as the field separator ๐ช FIELDWIDTHS and FPAT can also be used to control input field splitting, so there's more than one way to slice it ๐ see the details starting at https://www.gnu.org/software/gawk/manual/html_node/Fields.html.
each field in the input record can be referenced by its position: $1, $2, and so on ๐ $0 is the whole record, including any leading and trailing whitespace.
the variable NF is set to the total number of fields in the input record ๐ข
references to non-existent fields (aka fields after $NF) produce the null string, so nothing scary, just empty ๐ซง BUT assigning to a non-existent field (like $(NF+2) = 5) increases the value of NF, creates any intervening fields with the null string as their values, and causes $0 to be recomputed, with the fields separated by the value of OFS โจ referencing negative numbered fields causes a fatal error, so don't do that ๐ซ
decrementingNFmakes the values of fields past the new value get lost ๐ญ and$0gets recomputed too, with the fields separated by the value ofOFS. that's permanent, so be careful!
assigning a value to an existing field means the whole record gets rebuilt when $0 is referenced ๐ similarly, assigning a value to $0 causes the record to be resplit, creating new values for the fields. it's giving recycling, but make it cute ๐
๐ Comma Separated Values
when you run gawk with either the -k or the --csv option, it does NOT use regular record determination and field splitting like we just talked about ๐ instead, records are terminated by unquoted newlines, and fields are separated by commas ๐ฌ double-quotes can enclose fields containing commas, newlines, or doubled double-quotes. for more details, see https://www.gnu.org/software/gawk/manual/html_node/Comma-Separated-Fields.html ๐
๐งพ Built-in Variables
here are gawk's built-in variables! this list is purposely terse, so if you want the full tea, look at https://www.gnu.org/software/gawk/manual/html_node/Built_002din-Variables โ
ARGCโ the number of command line arguments ๐ขARGINDโ the index in ARGV of the current file being processed ๐ARGVโ array of command line arguments. it's indexed from 0 toARGC - 1๐งพBINMODEโ on non-POSIX systems, this specifies use of โbinaryโ mode for all file I/O ๐พ for the details, seehttps://www.gnu.org/software/gawk/manual/html_node/PC-Using.html.CONVFMTโ the conversion format for numbers,"%.6g"by default ๐ENVIRONโ an array containing the values of the current environment ๐ฟ it's indexed by the environment variables, with each element being the value of that variable.ERRNOโ if a system error happens while doing a redirection for getline, during a read for getline, or during aclose(), thenERRNOgets set to a string describing the error ๐ฌ the value is subject to translation in non-English locales, so it might look different for your bestie in another language ๐FIELDWIDTHSโ a whitespace-separated list of field widths ๐ when set, gawk parses the input into fixed-width fields, instead of usingFSas the field separator. each field width can optionally be preceded by a colon-separated value specifying how many characters to skip before the field starts ๐ฆFILENAMEโ the name of the current input file ๐ if no files are specified on the command line,FILENAMEis"-". BUTFILENAMEis undefined inside the BEGIN rule (unless it's set by getline), so don't rely on it there ๐FNRโ the input record number in the current input file ๐FPATโ a regular expression describing the contents of the fields in a record ๐งถ when set, gawk parses the input into fields where the fields match the regular expression, instead of usingFSas the field separator.FSโ the input field separator, a space by default โ๏ธ seehttps://www.gnu.org/software/gawk/manual/html_node/Field-Separators.htmlfor the details.FUNCTABโ an array whose indices and corresponding values are the names of all the user-defined or extension functions in the program ๐ง
NOTE: you may not use thedeletestatement with theFUNCTABarray. hands off! ๐
IGNORECASEโ controls the case-sensitivity of all regular expression and string operations ๐ seehttps://www.gnu.org/software/gawk/manual/html_node/Case_002dsensitivity.htmlfor details.LINTโ provides dynamic control of the--lintoption from inside an AWK program ๐งน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 โPRECโ the working precision of arbitrary precision floating-point numbers, 53 by default ๐งฎPROCINFOโ the elements of this array give you access to information about the running AWK program ๐ต๏ธ seehttps://www.gnu.org/software/gawk/manual/html_node/Auto_002dsetfor the details.ROUNDMODEโ the rounding mode for arbitrary precision arithmetic on numbers, by default"N"(IEEE-754 roundTiesToEven mode) ๐ฏ seehttps://www.gnu.org/software/gawk/manual/html_node/Setting-the-rounding-modefor the details.RSโ the input record separator, by default a newline ๐RTโ the record terminator ๐ gawk setsRTto the input text that matched the character or regular expression specified byRS.RSTARTโ the index of the first character matched bymatch();0if there's no match ๐ฏRLENGTHโ the length of the string matched bymatch();-1if there's no match ๐SUBSEPโ the string used to separate multiple subscripts in array elements, by default"\034"๐งทSYMTABโ an array whose indices are the names of all currently defined global variables and arrays in the program ๐๏ธ
you may not use thedeletestatement with theSYMTABarray, nor assign to elements with an index that isn't a variable name. no sneaky stuff! ๐ซ
TEXTDOMAINโ the text domain of the AWK program, used to find the localized translations for the program's strings ๐
Arrays
okay so arrays get subscripted with an expression inside square brackets, [ and ]. ๐งบ If that expression is an expression list (expr, expr ...), the subscript becomes one big string: the (string) value of each expression, glued together with whatever the SUBSEP variable holds in between. This is how gawk fakes multiply dimensioned arrays, and it's honestly so clever ๐ง โจ For example:
i = "A"; j = "B"; k = "C"
x[i, j, k] = "hello, world\n"
that assigns "hello, world\n" to the element of x indexed by the string "A\034B\034C". Every array in AWK is associative, meaning it's indexed by string values ๐
The special in operator checks whether an array has a particular index ๐
if (val in array)
print array[val]
If the array has multiple subscripts, use (i, j) in array instead.
The in construct can also be used in a for loop to step through every element of an array. BUT the (i, j) in array construct only works in tests, not in for loops, so don't try it there ๐
You can delete one element from an array with the delete statement. You can also wipe the entire array by using delete and just the array name, no subscript needed ๐งน
gawk supports true multidimensional arrays ๐ It does not require them to be "rectangular" the way C or C++ does, so your rows can be all different lengths and it's totally fine. For the deep dive, check https://www.gnu.org/software/gawk/manual/html_node/Arrays
Namespaces
gawk has a simple namespace facility ๐ It helps you work around the fact that every variable in AWK is global, which can get messy fast.
A qualified name is two simple identifiers joined by a double colon, ::. The left-hand identifier is the namespace, and the right-hand one is the variable living inside it. Simple (non-qualified) names all belong to the "current" namespace, and the default namespace is awk. BUT simple identifiers made up of only uppercase letters get forced into the awk namespace, even if your current namespace is something else. Iconic ๐
You switch the current namespace with an @namespace "name" directive.
The standard predefined builtin function names are NOT allowed as namespace names. The names of extra functions that gawk provides, though, can be used as namespace names or as simple identifiers in other namespaces. For more details, see https://www.gnu.org/software/gawk/manual/html_node/Namespaces.html#Namespaces.
Variable Typing And Conversion
Variables and fields can be (floating point) numbers, strings, or both ๐ They can also be regular expressions. What a value is depends on where it's used: in a numeric expression it gets treated as a number, and when it's used as a string it gets treated as a string. The context decides, bestie ๐
To force a variable to act like a number, add zero to it. To force it to act like a string, concatenate it with the null string "".
Uninitialized variables have the numeric value zero and the string value "" (the null, or empty, string) ๐ซง
When a string needs to become a number, gawk uses strtod(3) to do it. A number becomes a string by using the value of CONVFMT as a format string for sprintf(3), with the variable's numeric value as the argument. Here's the twist: even though every number in AWK is floating-point, integral values always get converted as integers โจ
Comparisons in gawk go like this. If both values are numeric, they're compared numerically. If one value is numeric and the other is a "numeric string," the comparison is also numeric. Otherwise, the numeric value gets converted to a string and a string comparison happens. Two strings are always compared as strings, obviously ๐
Quick heads up: string constants like "57" are NOT numeric strings. They're just string constants. The idea of a "numeric string" only applies to fields, getline input, FILENAME, ARGV elements, ENVIRON elements, and the elements of an array created by split() or patsplit() that are numeric strings. The vibe is basically this: user input, and only user input, that looks numeric should get treated that way ๐ซถ
Octal and Hexadecimal Constants
You can use C-style octal and hexadecimal constants right in your AWK program source ๐ข For example, the octal value 011 is equal to decimal 9, and the hexadecimal value 0x11 is equal to decimal 17. Math is serving ๐งฎ
String Constants
String constants in AWK are sequences of characters wrapped in double quotes, like "value". Inside strings, certain escape sequences get recognized, same as in C. For the details, see https://www.gnu.org/software/gawk/manual/html_node/Escape-Sequences ๐
Regexp Constants
A regular expression constant is a sequence of characters wrapped in forward slashes, like /value/. ๐ฏ
The escape sequences from the manual also work inside constant regular expressions. For example, /[ \t\f\n\r\v]/ matches whitespace characters ๐ซง
gawk also has strongly typed regular expression constants ๐ These get written with a leading @ symbol, like so: @/value/. You can assign these constants to scalars (variables and array elements) and pass them to user-defined functions. Any variable that got assigned one this way has regular expression type. Very fancy, very intentional ๐
PATTERNS AND ACTIONS
AWK is a line-oriented language ๐ The pattern comes first, and then the action. Action statements are enclosed in { and }. You can leave out the pattern, or leave out the action, but not both, obviously ๐ If the pattern is missing, the action runs for every single record of input. A missing action is the same as:
{ print }
which prints the entire record.
Comments begin with the # character and keep going until the end of the line ๐ฌ Empty lines are fine for separating statements. Normally a statement ends at a newline, EXCEPT for lines ending in a comma, {, ?, :, &&, or ||. Lines ending in do or else also automatically continue onto the next line. Everywhere else, you can continue a line by ending it with a \, and then the newline gets ignored. Just know that a \ after a # is not special ๐
You can cram multiple statements onto one line by separating them with a ;. This works both for the statements inside the action part of a pattern-action pair (the usual situation) and for the pattern-action statements themselves ๐
Patterns
AWK patterns can be any one of the following:
BEGIN
END
BEGINFILE
ENDFILE
/regular expression/
relational expression
pattern && pattern
pattern || pattern
pattern ? pattern : pattern
(pattern)
! pattern
pattern1, pattern2
BEGIN and END are two special kinds of patterns that don't get tested against the input ๐ง The action parts of all the BEGIN patterns get merged, as if every statement had been written in one single BEGIN rule. They run before any input is read. Same deal with END: all the END rules get merged, and they run once all the input is used up (or when an exit statement runs). BEGIN and END can't be combined with other patterns in pattern expressions, and they can't have missing action parts either ๐ซ
BEGINFILE and ENDFILE are extra special patterns whose actions run before the first record of each command-line input file is read, and after the last record of each file. Inside the BEGINFILE rule, the value of ERRNO is the empty string if the file opened successfully. Otherwise, something's wrong with the file, and you should use nextfile to skip it. If you don't do that, gawk throws its usual fatal error for files that can't be opened ๐ญ
For /regular expression/ patterns, the associated statement runs for each input record that matches the regular expression. Regular expressions here are basically the same as the ones in egrep(1). For the details on regular expressions, see https://www.gnu.org/software/gawk/manual/html_node/Regexp.html ๐
A relational expression can use any of the operators listed below in the Operators section. These usually test whether certain fields match certain regular expressions.
The &&, ||, and ! operators are logical AND, logical OR, and logical NOT, same as in C. They short-circuit evaluate, also just like C, and they're used for combining more basic pattern expressions. Like in most languages, parentheses let you change the order of evaluation ๐
The ?: operator works just like it does in C. If the first pattern is true, the pattern used for testing is the second pattern. Otherwise, it's the third. Only one of the second and third patterns gets evaluated, so no double work ๐ฆ
The pattern1, pattern2 form is called a range pattern. It matches all input records starting with a record that matches pattern1, and keeps going until a record that matches pattern2, inclusive, meaning that last record counts too. It doesn't combine with any other sort of pattern expression ๐ธ
Actions
Action statements are enclosed in braces, { and } ๐
They consist of the usual assignment, conditional, and looping statements you'd find in most languages. The operators, control statements, and input/output statements available are patterned after the ones in C, so if you know C you're already halfway there โจ
Operators
The operators in AWK, in order of decreasing precedence, are:
(...)โ Grouping ๐ง$โ Field reference.++,--โ Increment and decrement, both prefix and postfix.^โ Exponentiation ๐ฅ+,-,!โ Unary plus, unary minus, and logical negation.*,/,%โ Multiplication, division, and modulus โ๏ธ+,-โ Addition and subtraction.- space โ String concatenation. Literally just putting things next to each other ๐ซถ
|,|&โ Piped I/O forgetline,print, andprintf.<,>,<=,>=,==,!=โ The regular relational operators.~,!~โ Regular expression match, negated match.inโ Array membership.&&โ Logical AND.||โ Logical OR.?:โ The C conditional expression. This has the formexpr1 ? expr2 : expr3. Ifexpr1is true, the value of the expression isexpr2, otherwise it'sexpr3. Only one ofexpr2andexpr3gets evaluated ๐ฆ=,+=,-=,*=,/=,%=,^=โ Assignment. Both absolute assignment (var = value) and operator-assignment (the other forms) are supported ๐
Control Statements
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]
delete array
exit [ expression ]
{ statements }
switch (expression) {
case value|regex : statement
...
[ default: statement ]
}
โจ I/O Statements โจ
okay so here's where gawk gets its chat going ๐ฌ the input/output statements are all lined up below, bestie ๐ซถ
close(file [, how])โ closes an open file, pipe, or coprocess ๐ the optionalhowshould ONLY be used when you're closing one end of a two-way pipe to a coprocess, and it has to be a string, either"to"or"from"๐getlineโ sets$0from the next input record, and setsNF,NR,FNR, andRT๐ฅgetline <fileโ sets$0from the next record of file, and setsNFandRT๐getline varโ sets var from the next input record, and setsNR,FNR, andRT๐getline var <fileโ sets var from the next record of file, and setsRT๐งcommand | getline [var]โ runs command and pipes its output into$0or var (same as above), plusRT๐ธcommand |& getline [var]โ runs command as a coprocess and pipes its output into$0or var (same as above), plusRT๐ฆ the command can also be a socket, so peep the subsection Special File Names below ๐fflush([file])โ flushes any buffers tied to the open output file or pipe ๐ฟ if file is missing or it's the null string, gawk flushes ALL open output files and pipes, iconic behavior ๐nextโ stops processing the current input record, reads the next one, and starts over from the first pattern in the AWK program โญ once it hits the end of the input data, it runs anyENDrule(s) ๐nextfileโ stops processing the current input file, so the next record read comes from the next input file ๐ it updatesFILENAMEandARGIND, resetsFNRto 1, and restarts from the first pattern in the AWK program ๐ at the end of the input data, it runs anyENDFILEandENDrule(s) ๐printโ prints the current record, terminated with the value ofORS๐จ๏ธprint expr-listโ prints expressions, each one separated by the value ofOFS, and the output record gets terminated with the value ofORS๐print expr-list >fileโ same as above, but the expressions get printed on file ๐printf fmt, expr-listโ formats and prints ๐printf fmt, expr-list >fileโ formats and prints on file ๐system(cmd-line)โ executes the command cmd-line and returns its exit status ๐ป this may not be available on non-POSIX systems, so heads up ๐ for the full scoop on the exit status, checkhttps://www.gnu.org/software/gawk/manual/html_node/I_002fO-Functions.html#I_002fO-Functions๐
Additional output redirections are allowed for print and printf, and they're lowkey so useful ๐ซถ
print ... >> fileโ appends output to the file ๐print ... | commandโ writes on a pipe ๐ฐprint ... |& commandโ sends data to a coprocess or socket ๐ฆ (see also the subsection Special File Names, below)
The getline function returns 1 on success, 0 on end of file, and -1 on an error โ if the errno(3) value says the I/O operation may be retried, AND PROCINFO["input", "RETRY"] is set, then -2 gets returned instead of -1, and you can try calling getline again ๐ when there's an error, ERRNO gets set to a string describing the problem ๐ญ
NOTE, and this is IMPORTANT bestie ๐จ failing to open a two-way socket gives you a non-fatal error that gets returned to the calling function ๐ if you're using a pipe, coprocess, or socket withgetline, or withprintfinside a loop, you MUST useclose()to create new instances of the command or socket ๐ AWK does NOT automatically close pipes, sockets, or coprocesses when they return EOF, so don't count on it ๐ซ
The AWK versions of the printf statement and sprintf() function are basically the same as C's ๐ธ for the details, peep https://www.gnu.org/software/gawk/manual/html_node/Printf.html ๐
โจ Special File Names โจ
okay so when you do I/O redirection from print or printf into a file, or use getline from a file, gawk recognizes certain special filenames internally ๐ง these let you access open file descriptors inherited from gawk's parent process (usually the shell) ๐ you can also use these filenames on the command line to name data files ๐ here they are, bestie:
-โ the standard input ๐ฅ/dev/stdinโ the standard input ๐ฅ/dev/stdoutโ the standard output ๐ค/dev/stderrโ the standard error output ๐จ/dev/fd/nโ the file associated with the open file descriptor n ๐
The following special filenames can be used with the |& coprocess operator for making TCP/IP network connections ๐ slay:
/inet/tcp/lport/rhost/rport,/inet4/tcp/lport/rhost/rport,/inet6/tcp/lport/rhost/rportโ files for a TCP/IP connection on local port lport to remote host rhost on remote port rport ๐ฆ use a port of0to have the system pick one for you ๐ฒ use/inet4to force an IPv4 connection, and/inet6to force an IPv6 connection ๐ plain/inetuses the system default (most likely IPv4) ๐ these are usable ONLY with the|&two-way I/O operator ๐ซถ/inet/udp/lport/rhost/rport,/inet4/udp/lport/rhost/rport,/inet6/udp/lport/rhost/rportโ same deal, but with UDP/IP instead of TCP/IP โจ
โจ Numeric Functions โจ
AWK has the following built-in arithmetic functions, and they're giving math-class-but-make-it-fun ๐งฎ๐
atan2(y, x)โ returns the arctangent ofy/xin radians ๐cos(expr)โ returns the cosine of expr, which is in radians ๐exp(expr)โ the exponential function ๐int(expr)โ truncates to an integer โ๏ธlog(expr)โ the natural logarithm function ๐ชตrand()โ returns a random number N, between zero and one, such that0 โค N < 1๐ฒโจsin(expr)โ returns the sine of expr, which is in radians ๐sqrt(expr)โ returns the square root of expr ๐srand([expr])โ uses expr as the new seed for the random number generator ๐ฑ if no expr is provided, it uses the time of day โฐ returns the previous seed for the random number generator ๐
โจ String Functions โจ
Gawk has the following built-in string functions, and the full tea on them is over at https://www.gnu.org/software/gawk/manual/html_node/String-Functions ๐ bestie, these are the main vibes ๐ซถ
asort(s [, d [, how] ])โ returns the number of elements in the source array s ๐ง it sorts the contents of s using gawk's normal rules for comparing values, then replaces the indices of the sorted values in s with sequential integers starting at 1 ๐ข if the optional destination array d is specified, gawk first duplicates s into d, then sorts d, leaving the indices of the source array s unchanged ๐ the optional string how controls the direction and the comparison mode, and the valid values for how are described inhttps://www.gnu.org/software/gawk/manual/html_node/String-Functions.html#String-Functions๐ s and d are allowed to be the same array, but that only makes sense when you supply the third argument too ๐คasorti(s [, d [, how] ])โ returns the number of elements in the source array s ๐ญ it works the same asasort(), except the array indices are used for sorting, not the array values ๐ when it's done, the array is indexed numerically, and the values are the original indices ๐ซ the original values are lost, so if you want to keep them, give it a second array ๐พ the purpose of the optional string how is the same as forasort()๐ again, s and d can be the same array, but only when you supply the third argument as well โจgensub(r, s, h [, t])โ searches the target string t for matches of the regular expression r ๐ if h is a string beginning withgorG, it replaces ALL matches of r with s ๐ otherwise, h is a number telling gawk which match of r to replace ๐ฏ if t isn't supplied, gawk uses$0instead ๐ inside the replacement text s, the sequence\n, where n is a digit from 1 to 9, can be used to mean just the text that matched the n'th parenthesized subexpression ๐งฉ the sequence\0represents the entire matched text, same as the character&๐ unlikesub()andgsub(), the modified string gets returned as the result of the function, and the original target string is NOT changed ๐ซถgsub(r, s [, t])โ for each substring matching the regular expression r in the string t, it substitutes the string s, and returns the number of substitutions ๐ if t isn't supplied, it uses$0๐ an&in the replacement text gets replaced with the text that was actually matched ๐ to get a literal&, use\&, which has to be typed as"\\&"๐ for a fuller discussion of the rules for ampersands and backslashes in the replacement text ofsub(),gsub(), andgensub(), checkhttps://www.gnu.org/software/gawk/manual/html_node/Gory-Details.html#Gory-Details๐ธ
index(s, t) hunts for the string t inside s and hands back its position, or zero if t isn't there at all ๐ (which also means character positions start at one, not zero, bestie ๐ )
length([s])โ gives you the length of the string s, or the length of$0if you don't pass anything in. Feed it an array and it tells you how many elements that array holds ๐งฎmatch(s, r [, a])โ finds where the regular expression r shows up in s and returns that position, or zero if it's a no-show. It also setsRSTARTandRLENGTHfor you โจ Heads up, the argument order matches the~operator, so it'sstr ~ re๐ For the full scoop on how the array a gets filled when you include it, checkhttps://www.gnu.org/software/gawk/manual/html_node/String-Functions.html#String-Functions๐patsplit(s, a [, r [, seps] ])โ splits s into the array a and the separators array seps using the regex r, then returns the number of fields ๐ฆ The elements of a are the chunks of s that matched r.seps[i]is the separator that came right aftera[i](it might be empty, that's fine!).seps[0]is the possibly-empty separator at the very front ๐ธ If you skip r,FPATgets used instead. Both arrays get wiped clean before anything happens, and splitting works exactly like field splitting withFPAT๐ซถsplit(s, a [, r [, seps] ])โ same vibe as above, splitting s into a and seps on the regex r, and returning the number of fields ๐ If you leave r out,FSsteps in. Both arrays get cleared first. This timeseps[i]is the field separator that r matched betweena[i]anda[i+1], and splitting acts just like regular field splitting ๐sprintf(fmt, expr-list)โ prints expr-list using the format fmt and gives you back the resulting string ๐strtonum(str)โ reads str and returns its numeric value ๐ข A leading0means octal. A leading0xor0Xmeans hexadecimal. Anything else gets treated as decimal ๐sub(r, s [, t])โ basicallygsub(), but it only replaces the first matching substring ๐ It returns either zero or one, so no drama ๐substr(s, i [, n])โ returns a substring of s starting at position i that is at most n characters long โ๏ธ If you leave n out, you get everything from i to the end of s ๐ฆtolower(str)โ returns a copy of str with every uppercase character turned into its lowercase buddy. Non-alphabetic characters just chill and stay unchanged ๐งtoupper(str)โ returns a copy of str with every lowercase character glowed up to uppercase. Non-alphabetic characters stay exactly as they are ๐ฃ
Gawk is multibyte aware, which is so iconic ๐ That means index(), length(), substr() and match() all count in characters, not bytes ๐ซถ
โฐ Time Functions
Gawk gives you functions for grabbing timestamps and formatting them โฐ The deets live at https://www.gnu.org/software/gawk/manual/html_node/Time-Functions.
mktime(datespec [, utc-flag])โ turns datespec into a timestamp of the same form thatsystime()returns, and hands it back ๐ฐ๏ธ If utc-flag is there and is non-zero or non-null, the time is treated as UTC. Otherwise it's assumed to be local time. If datespec doesn't have enough elements, or the resulting time is out of range,mktime()returns -1 ๐ For the lowdown on datespec, seehttps://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html#Time-Functions๐strftime([format [, timestamp[, utc-flag]]])โ formats timestamp according to format ๐จ If utc-flag is present and is non-zero or non-null, you get UTC. Otherwise it's local time. The timestamp should look like whatsystime()returns. No timestamp? It uses the current time of day โณ No format? You get a default that matches the output ofdate(1)๐๏ธ That default lives inPROCINFO["strftime"]๐ For the format conversions you can count on, look at thestrftime()spec in ISO C ๐systime()โ returns the current time of day as the number of seconds since the Epoch, which is1970-01-01 00:00:00 UTCon POSIX systems ๐
๐ข Bit Manipulations Functions
Gawk has bit manipulation functions for the nerdy stuff ๐ค They work by converting double-precision floating point values to uintmax_t integers, doing the operation, and then converting the result back to floating point. Passing negative operands to any of these functions causes a fatal error ๐ So don't do it!
The functions are:
and(v1, v2 [, ...])โ returns the bitwise AND of the values in the argument list. You need at least two ๐งฉcompl(val)โ returns the bitwise complement of val ๐lshift(val, count)โ returns val shifted left by count bits โฌ ๏ธor(v1, v2 [, ...])โ returns the bitwise OR of the values in the argument list. Again, at least two are required ๐งฉrshift(val, count)โ returns val shifted right by count bits โก๏ธxor(v1, v2 [, ...])โ returns the bitwise XOR of the values in the argument list. Yep, at least two ๐งฉ
๐งธ Type Functions
These functions spill the tea on the type of their arguments โ
isarray(x)โ returns true if x is an array, false otherwise ๐ฆtypeof(x)โ returns a string telling you the type of x. That string will be one of"array","number","regexp","string","strnum","unassigned", or"undefined"๐ท๏ธ
๐ Internationalization Functions
These functions can be used inside your AWK program to translate strings while it's running ๐ฃ๏ธ For the full details, go check https://www.gnu.org/software/gawk/manual/html_node/I18N-Functions.html#I18N-Functions ๐
bindtextdomain(directory [, domain])โ tells gawk which directory to look in for the.gmofiles, for when they can't or won't live in the "standard" locations ๐ It returns the directory where domain is โbound.โ The default domain is the value ofTEXTDOMAIN. If directory is the null string (""), thenbindtextdomain()returns the current binding for the given domain ๐dcgettext(string [, domain [, category]])โ returns the translation of string in the text domain domain for the locale category category ๐ The default for domain is the current value ofTEXTDOMAIN. The default for category is"LC_MESSAGES"๐ฌdcngettext(string1, string2, number [, domain [, category]])โ returns the plural form used for number of the translation of string1 and string2 in the text domain domain for the locale category category ๐ข The default for domain is the current value ofTEXTDOMAIN. The default for category is"LC_MESSAGES"๐ซถ
โ Boolean Valued Functions
You can make special Boolean-typed values! Check the manual for how they work and why they exist ๐ง
mkbool(expression)โ based on the boolean value of expression, returns either a true value or a false value ๐ฏ True values have numeric value one. False values have numeric value zero 0๏ธโฃ
โจ USER-DEFINED FUNCTIONS โจ
Functions in AWK are defined like this ๐
function name(parameter list) { statements }
Functions run when they get called from inside expressions in either patterns or actions ๐ฌ The actual parameters you pass in the call are used to fill in the formal parameters declared in the function. Arrays are passed by reference, and other variables are passed by value ๐
Local variables get declared as extra parameters in the parameter list. The convention is to separate them from the real parameters with extra spaces, so it's easy to spot them ๐ For example:
function f(p, q, a, b) # a and b are local
{
...
}
/abc/ { ... ; f(1, 2) ; ... }
The left parenthesis in a function call has to come right after the function name, with no whitespace in between ๐ โโ๏ธ This restriction doesn't apply to the built-in functions listed above ๐ซถ
Functions can call each other, and they can even be recursive ๐ Function parameters that get used as local variables are initialized to the null string and the number zero each time the function is invoked ๐งธ
Use return expr to send a value back from a function ๐ The return value is undefined if no value is provided, or if the function returns by โfalling offโ the end ๐ป
Functions can also be called indirectly, which is giving main character energy ๐ To do this, assign the name of the function you want to call, as a string, to a variable. Then use that variable as if it were the function name, prefixed with an @ sign, like so:
function myfunc()
{
print "myfunc called"
...
}
{ ...
the_func = "myfunc"
@the_func() # call through the_func to myfunc
...
}
If --lint has been provided, gawk warns about calls to undefined functions at parse time, instead of at run time ๐ Calling an undefined function at run time is a fatal error ๐
๐ DYNAMICALLY LOADING NEW FUNCTIONS ๐
You can dynamically add new functions written in C or C++ to the running gawk interpreter with the @load statement ๐งฉ The full details are beyond the scope of this manual page, but you can find them at https://www.gnu.org/software/gawk/manual/html_node/Dynamic-Extensions.html#Dynamic-Extensions ๐
โจ SIGNALS โจ
okay so the gawk profiler actually takes two signals ๐
SIGUSR1 makes it dump the profile and the function call stack into the profile file (which is awkprof.out by default, or whatever file you named with the --profile option) and then it just keeps on running ๐โโ๏ธ SIGHUP is the dramatic one ๐ญ it dumps the profile and function call stack and then exits right away.
๐ธ INTERNATIONALIZATION ๐ธ
String constants are just sequences of characters wrapped in double quotes ๐ Now, in non-English speaking environments you can mark a string in your AWK program as needing translation into the local natural language ๐ You do this with a leading underscore (โ_โ) in front of the string. For example,
gawk 'BEGIN { print "hello, world" }'
always prints hello, world ๐ But,
gawk 'BEGIN { print _"hello, world" }'
might print bonjour, monde in France ๐ซ๐ท โจ For the steps involved in producing and running a localizable AWK program, check out https://www.gnu.org/software/gawk/manual/html_node/Internationalization.html#Internationalization
๐ฆ GNU EXTENSIONS ๐ฆ
Gawk has a seriously huge number of extensions on top of POSIX awk ๐ญ They're all described in https://www.gnu.org/software/gawk/manual/html_node/POSIX_002fGNU.html. Want none of them? Invoke gawk with the --traditional or --posix option and every extension gets switched off ๐
โโ๏ธ
๐ง ENVIRONMENT VARIABLES ๐ง
The AWKPATH environment variable is a list of directories that gawk searches when it's looking for files named via the -f, --file, -i and --include options, and the @include directive ๐ If the first search fails, gawk searches again after appending .awk to the filename.
The AWKLIBPATH environment variable is a list of directories that gawk searches when looking for files named via the -l and --load options ๐
The GAWK_PERSIST_FILE environment variable, if it's present, names a file to use as the backing store for persistent memory ๐ For the details, see GAWK: Effective AWK Programming.
The GAWK_READ_TIMEOUT environment variable lets you set a timeout in milliseconds for reading input from a terminal, pipe or two-way communication, including sockets โณ
For connecting to a remote host via socket, GAWK_SOCK_RETRIES controls the number of retries and GAWK_MSEC_SLEEP controls the interval between retries ๐ซถ The interval is in milliseconds. On systems that don't support usleep(3), the value gets rounded up to a whole number of seconds.
If POSIXLY_CORRECT exists in the environment, gawk behaves exactly as if --posix had been passed on the command line ๐ If --lint is also specified, gawk issues a warning message about this ๐
๐ช EXIT STATUS ๐ช
If the exit statement is used with a value, gawk exits with that numeric value, no questions asked โจ
Otherwise, if nothing went wrong during execution, gawk exits with the value of the C constant EXIT_SUCCESS, which is usually zero ๐ฅณ
If an error occurs, gawk exits with the value of the C constant EXIT_FAILURE, which is usually one ๐ฌ
If gawk exits because of a fatal error, the exit status is 2. On non-POSIX systems, this value may get mapped to EXIT_FAILURE.
๐ VERSION INFORMATION ๐
This man page documents gawk, version 5.3 ๐
๐ AUTHORS ๐
The original version of UNIX awk was designed and implemented by Alfred Aho, Peter Weinberger, and Brian Kernighan of Bell Laboratories ๐ฉโ๐ฌ Ozan Yigit is the current maintainer. Brian Kernighan occasionally dabbles in its development, iconic behavior ๐
Paul Rubin and Jay Fenlason, of the Free Software Foundation, wrote gawk to be compatible with the original version of awk distributed in Seventh Edition UNIX ๐งโ๐ป John Woods contributed a bunch of bug fixes. David Trueman, with contributions from Arnold Robbins, made gawk compatible with the new version of UNIX awk. Arnold Robbins is the current maintainer ๐
See GAWK: Effective AWK Programming for a full list of the contributors to gawk and its documentation ๐ See the README file in the gawk distribution for up-to-date information about maintainers and which ports are currently supported ๐
๐ BUG REPORTS AND QUESTIONS ๐
If you find a bug in gawk, please use the gawkbug(1) program to report it ๐
Full instructions for reporting a bug are in https://www.gnu.org/software/gawk/manual/html_node/Bugs.html. Please carefully read and follow the instructions given there. This makes bug reporting and resolution way easier for everyone involved. Really ๐ซ
On the other hand, if you have a question about how to accomplish a particular task using awk or gawk, email help-gawk@gnu.org with your request for help ๐
๐ BUGS ๐
The -F option isn't necessary given the command line variable assignment feature. It only sticks around for backwards compatibility, so don't stress about it.
This manual page is too long, and gawk has too many features ๐
๐ SEE ALSO ๐
egrep(1), sed(1), gawkbug(1), printf(3), and strftime(3) ๐
The AWK Programming Language, second edition, Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger, Addison-Wesley, 2023. ISBN 9-780138-269722 ๐
GAWK: Effective AWK Programming, Edition 5.3, shipped with the gawk source. The current version of this document is online at https://www.gnu.org/software/gawk/manual ๐
The GNU gettext documentation, available online at https://www.gnu.org/software/gettext ๐ธ
๐ญ EXAMPLES ๐ญ
Print and sort the login names of all users:
BEGIN { FS = ":" }
{ print $1 | "sort" }
Count lines in a file:
{ nlines++ }
BEGIN { print nlines }
Precede each line by its number in the file:
{ print FNR, $0 }
Concatenate and line number (a variation on a theme):
{ print NR, $0 }
Run an external command for particular lines of data:
tail -f access_log |
awk '/myhome.html/ { system("nmap " $1 ">> logdir/myhome.html") }'
๐ COPYING PERMISSIONS ๐
Copyright ยฉ 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2007, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025 Free Software Foundation, Inc. ๐ฝ
Permission is granted to make and distribute verbatim copies of this manual page provided the copyright notice and this permission notice are preserved on all copies ๐ซถ
Permission is granted to copy and distribute modified versions of this manual page under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one ๐
Permission is granted to copy and distribute translations of this manual page into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation ๐
bestie, that's the whole thing ๐ Free Software Foundation, March 23 2025, GAWK(1) ๐ now go be a awk legend โจ๐ฆ
๐ฆ where this page comes from
- fedora 43: gawk 5.3.2-2.fc43 ยท GPL-3.0-or-later AND GPL-2.0-or-later AND LGPL-2.1-or-later AND BSD-3-Clause
- fedora 43: gawk 5.3.2-2.fc43 ยท GPL-3.0-or-later AND GPL-2.0-or-later AND LGPL-2.1-or-later AND BSD-3-Clause
- fedora 44: gawk 5.3.2-3.fc44 ยท GPL-3.0-or-later AND GPL-2.0-or-later AND LGPL-2.1-or-later AND BSD-3-Clause
- fedora 44: gawk 5.3.2-3.fc44 ยท GPL-3.0-or-later AND GPL-2.0-or-later AND LGPL-2.1-or-later AND BSD-3-Clause
- opensuse-leap 16.0: gawk 5.3.2-160000.3.1 ยท GPL-3.0-or-later
- opensuse-leap 16.0: gawk 5.3.2-160000.3.1 ยท GPL-3.0-or-later
- opensuse-leap 16.0: gawk 5.3.2-160000.3.1 ยท GPL-3.0-or-later
- opensuse-leap 16.0: gawk 5.3.2-160000.3.1 ยท GPL-3.0-or-later
- ubuntu questing: gawk 1:5.3.2-1build1 ยท GPL-3.0-or-later
- ๐ girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms