Complete awk commands reference — 48 commands across 2 categories. Quick reference cheat sheet for Linux, macOS & BSD.
awk is a small language for processing text a record at a time, and this page is its GNU implementation, gawk: the command-line options and the built-in variables that control how input is split and output is joined. Most one-liners use two options and three variables. The notes cover those and the traps that come from not knowing what a "field" is.
| Shortcut | Action |
|---|---|
| awk -f program-file | Read the AWK program source from the file program-file, instead of from the first command |
| awk -F fs | Use fs for the input field separator (the value of the FS predefined variable) |
| awk -v var=val | Assign the value val to the variable var, before execution of the program begins |
| awk -b | Treat all input data as single-byte characters |
| awk -c | Run in compatibility mode |
| awk -C | Print the short version of the GNU copyright information message on the standard output |
| awk -d[file] | Print a sorted list of global variables, their types and final values to file |
| awk -D[file] | Enable debugging of AWK programs |
| awk -e program-text | Use program-text as AWK program source code |
| awk -E file | Similar to -f, however, this option is the last one processed |
| awk -g | Scan and parse the AWK program, and generate a GNU .pot (Portable Object Template) format |
| awk -h | Print a relatively short summary of the available options on the standard output |
| awk -i include-file | Load an awk source library |
| awk -I | Print the internal byte code names as they are executed when running the program |
| awk -k | Enable CSV special processing |
| awk -l lib | Load a gawk extension from the shared library lib |
| awk -L [value] | Provide warnings about constructs that are dubious or non- portable to other AWK implement |
| awk -M | Force arbitrary precision arithmetic on numbers |
| awk -n | Recognize octal and hexadecimal values in input data |
| awk -N | Force gawk to use the locale's decimal point character when parsing input data |
| awk -o[file] | Output a pretty printed version of the program to file |
| awk -O | Enable gawk's default optimizations upon the internal representation of the program |
| awk -p[prof-file] | Start a profiling session, and send the profiling data to prof-file |
| awk -P | This turns on compatibility mode, and disables a number of common extensions |
| awk -r | Enable the use of interval expressions in regular expression matching |
| awk -s | Disable gawk's default optimizations upon the internal representation of the program |
| awk -S | Run gawk in sandbox mode, disabling the system() function, input redirection with getline |
| awk -t | Provide warnings about constructs that are not portable to the original version of UNIX aw |
| awk -V | Print version information for this particular copy of gawk on the standard output |
| awk -- | Signal the end of options |
| Shortcut | Action |
|---|---|
| ARGV | Array of command line arguments |
| BINMODE | On non-POSIX systems, specifies use of "binary" mode for all file I/O |
| CONVFMT | The conversion format for numbers, "%.6g", by default |
| ENVIRON | An array containing the values of the current environment |
| ERRNO | If a system error occurs either doing a redirection for getline, during a read for getline |
| FIELDWIDTHS | A whitespace-separated list of field widths |
| FILENAME | The name of the current input file |
| FPAT | A regular expression describing the contents of the fields in a record |
| FS | The input field separator, a space by default |
| FUNCTAB | An array whose indices and corresponding values are the names of all the user-defined or e |
| IGNORECASE | Controls the case-sensitivity of all regular expression and string operations |
| LINT | Provides dynamic control of the --lint option from within an AWK program |
| PREC | The working precision of arbitrary precision floating-point numbers, 53 by default |
| PROCINFO | The elements of this array provide access to information about the running AWK program |
| ROUNDMODE | The rounding mode to use for arbitrary precision arithmetic on numbers, by default "N" (IE |
| RT | The record terminator |
| RLENGTH | The length of the string matched by match(); -1 if no match |
| TEXTDOMAIN | The text domain of the AWK program; used to find the localized translations for the progra |
awk splits each line into fields on whitespace, numbered $1, $2… with $0 the whole line. awk -F fs changes the input separator — -F: for /etc/passwd, -F, for simple CSV — and sets the FS variable. awk -v var=val passes a shell value into the program before it runs, which is the correct way to use a shell variable inside single quotes. awk -f program-file reads the program from a file once a one-liner has grown past a line. Output fields are joined by OFS, which defaults to a space, so -v OFS=, turns a print of several fields into comma-separated output.
FILENAME is the current input file — useful with several files on the command line — and the record counters NR and FNR (total and per-file) let NR==FNR distinguish the first file from the rest in two-file joins. RT holds the text that terminated the current record. ENVIRON is an array of the environment, so ENVIRON["HOME"] reads a variable without -v. RLENGTH holds the length of the last match(), and IGNORECASE set to 1 makes every comparison and regex case-insensitive in gawk.
Proper CSV with quoted commas defeats -F,; gawk 5.3 added awk -k to enable CSV parsing, and older versions use FPAT with a pattern that matches quoted fields. FIELDWIDTHS splits fixed-width records by column counts instead of separators. awk -M switches to arbitrary-precision arithmetic for numbers that overflow doubles, awk -L [value] warns about non-portable constructs when a script must also run on mawk or BSD awk, and awk -c runs in compatibility mode with the extensions off.
Pass it with -v: awk -v name="$NAME" '$1 == name' file. Do not interpolate it into the quoted program.
Set OFS: awk -F: -v OFS="\t" '{print $1, $3}' /etc/passwd. The comma in print uses OFS between fields.
Use gawk's -k option (5.3 and later) or set FPAT to a pattern for quoted fields. Plain -F, splits inside quotes.
NR counts records across all input files; FNR resets for each file. NR==FNR is true only while reading the first file.
Open your assistant with this page preloaded as the source — great for follow-up questions like "which of these work in other apps?"