# man > awk

---
type: CommandReference
command: gawk
mode: man
section: 1
source: man-pages
---

## Quick Reference
- `awk '{print $5}' {{path/to/file}}` — Print the fifth column.
- `awk '/{{foo}}/ {print $2}' {{path/to/file}}` — Print second column of lines containing "foo".
- `awk -F ',' '{print $NF}' {{path/to/file}}` — Print last column using comma as field separator.
- `awk '{s+=$1} END {print s}' {{path/to/file}}` — Sum values in the first column and print the total.
- `awk 'NR%3==1' {{path/to/file}}` — Print every third line starting from the first.
- `awk '{if ($1 == "foo") print "Exact match foo"; else if ($1 ~ "bar") print "Partial match bar"; else print "Baz"}' {{path/to/file}}` — Print different values based on conditions.
- `awk '($10 >= {{min_value}} && $10 <= {{max_value}})' {{path/to/file}}` — Print lines where 10th column is between a min and max.
- `awk 'BEGIN {FS=":";printf "%-20s %6s %25s\n", "Name", "UID", "Shell"} $4 >= 1000 {printf "%-20s %6d %25s\n", $1, $4, $7}' /etc/passwd` — Print table of users with UID ≥1000, formatted with colon separator.

## Name
gawk — pattern scanning and processing language

## Synopsis
shell
gawk [options] -f program-file [--] file ...
gawk [options] [--] program-text file ...
**AWK program structure**:
@include "filename"
@load "filename"
@namespace "name"
pattern { action statements }
function name(parameter list) { statements }
Patterns: `BEGIN`, `END`, `BEGINFILE`, `ENDFILE`, regular expressions (`/re/`), relational expressions, logical combinations (`&&`, `||`, `!`), conditional (`?:`), range (`pat1, pat2`). Actions: statements in braces, separated by newlines or semicolons. Comments start with `#`.

**Execution order**: `-v` assignments → compile → `BEGIN` rule(s) → process each file (`BEGINFILE`/`ENDFILE`) → `END` rule(s). Variable assignments on command line (`var=val`) are performed after `BEGIN` and before file processing.

**Records and fields**: Records separated by `RS` (default newline). Fields are split by `FS` (default space), or by `FIELDWIDTHS` (fixed widths), or by `FPAT` (regexp describing fields). `$0` is the whole record, `$1`..`$NF` are fields. `NF` is number of fields, `NR` is total records, `FNR` is records in current file.

**Built‑in variables**:
- `ARGC` — number of command-line arguments.
- `ARGIND` — index in `ARGV` of current file.
- `ARGV` — array of command-line arguments.
- `BINMODE` — binary mode for file I/O (non‑POSIX); values: 1,2,3 or `"r"`,`"w"`,`"rw"`.
- `CONVFMT` — number conversion format (default `"%.6g"`).
- `ENVIRON` — array of environment variables.
- `ERRNO` — string describing last system error.
- `FIELDWIDTHS` — space‑separated list of fixed field widths.
- `FILENAME` — current input file name (`"-"` for stdin).
- `FNR` — input record number in current file.
- `FPAT` — regexp describing fields (overrides `FS`).
- `FS` — input field separator (default space).
- `FUNCTAB` — array of all user/extension function names (cannot `delete`).
- `IGNORECASE` — if non‑zero, case‑insensitive matching for regexps, `FS`, `RS`, etc.
- `LINT` — dynamic control of `--lint` option.
- `NF` — number of fields in current record.
- `NR` — total number of input records seen.
- `OFMT` — output number format (default `"%.6g"`).
- `OFS` — output field separator (default space).
- `ORS` — output record separator (default newline).
- `PREC` — working precision for arbitrary‑precision arithmetic (default 53).
- `PROCINFO` — array of process/run‑time information. Key elements:
  - `PROCINFO["argv"]`, `PROCINFO["egid"]`, `PROCINFO["errno"]`, `PROCINFO["euid"]`, `PROCINFO["FS"]`, `PROCINFO["gid"]`, `PROCINFO["identifiers"]` (subarray of identifier types), `PROCINFO["pgrpid"]`, `PROCINFO["pid"]`, `PROCINFO["platform"]`, `PROCINFO["ppid"]`, `PROCINFO["strftime"]`, `PROCINFO["uid"]`, `PROCINFO["version"]`.
  - `PROCINFO["api_major"]`, `PROCINFO["api_minor"]` (extension API version).
  - `PROCINFO["gmp_version"]`, `PROCINFO["mpfr_version"]`, `PROCINFO["prec_max"]`, `PROCINFO["prec_min"]` (MPFR support).
  - Settable: `PROCINFO["NONFATAL"]`, `PROCINFO["name","NONFATAL"]`, `PROCINFO["command","pty"]`, `PROCINFO["input","READ_TIMEOUT"]`, `PROCINFO["input","RETRY"]`, `PROCINFO["sorted_in"]`.
- `ROUNDMODE` — rounding mode for arbitrary precision (`"A"`,`"D"`,`"N"`,`"U"`,`"Z"`; default `"N"`).
- `RS` — input record separator (default newline).
- `RT` — record terminator (text matched by `RS`).
- `RSTART` — index of first character matched by `match()`; 0 if none.
- `RLENGTH` — length of matched string; -1 if no match.
- `SUBSEP` — subscript separator for multidimensional arrays (default `"\034"`).
- `SYMTAB` — array of all global variables; indirect access via `SYMTAB["var"]`.
- `TEXTDOMAIN` — text domain for internationalization.

**Built‑in functions**:
- Numeric: `atan2(y,x)`, `cos(expr)`, `exp(expr)`, `int(expr)`, `log(expr)`, `rand()`, `sin(expr)`, `sqrt(expr)`, `srand([expr])` (returns previous seed).
- String:
  - `asort(s [, d [, how]])` — sort array `s` by values, optionally into `d`.
  - `asorti(s [, d [, how]])` — sort by indices.
  - `gensub(r, s, h [, t])` — replace matches of regexp `r` in `t` with `s`; `h` is `"g"`/`"G"` or a number; returns modified string.
  - `gsub(r, s [, t])` — replace all matches in `t` (default `$0`), return count.
  - `index(s, t)` — position of `t` in `s` (1‑based), 0 if not found.
  - `length([s])` — length of string `s` (or `$0`); with an array argument returns number of elements.
  - `match(s, r [, a])` — position where `r` matches in `s`, sets `RSTART`/`RLENGTH`; fills array `a` with subexpression captures.
  - `patsplit(s, a [, r [, seps]])` — split `s` into fields matching `r` (default `FPAT`), stores separators in `seps`.
  - `split(s, a [, r [, seps]])` — split `s` on `r` (default `FS`), stores separators in `seps`.
  - `sprintf(fmt, expr-list)` — format and return string.
  - `strtonum(str)` — convert string to number (supports 0, 0x, 0X prefixes).
  - `sub(r, s [, t])` — replace first match, return 0 or 1.
  - `substr(s, i [, n])` — substring of `s` starting at `i` (1‑based), length `n`.
  - `tolower(str)`, `toupper(str)` — case conversion.
- Time:
  - `mktime(datespec [, utc-flag])` — convert `"YYYY MM DD HH MM SS [DST]"` to epoch seconds.
  - `strftime([format [, timestamp [, utc-flag]]])` — format time.
  - `systime()` — current epoch seconds.
- Bit manipulation: `and(v1, v2, ...)`, `compl(val)`, `lshift(val, count)`, `or(v1, v2, ...)`, `rshift(val, count)`, `xor(v1, v2, ...)`.
- Type: `isarray(x)`, `typeof(x)` (returns `"array"`, `"number"`, `"regexp"`, `"string"`, `"strnum"`, `"unassigned"`, `"undefined"`).
- Internationalization: `bindtextdomain(directory [, domain])`, `dcgettext(string [, domain [, category]])`, `dcngettext(string1, string2, number [, domain [, category]])`.

**Regular expressions** (extended, like `egrep`):
- `.` any char; `^` begin; `$` end; `[...]` char list (`[^...]` negated); `|` alternation; `*`, `+`, `?` quantifiers; `()` grouping; `{n}`, `{n,m}`, `{n,}` interval expressions.
- Escape sequences: `\a` alert, `\b` backspace, `\f` form‑feed, `\n` newline, `\r` carriage return, `\t` tab, `\v` vertical tab, `\xHH` hex, `\ooo` octal, `\\` backslash.
- Character classes inside `[...]`: `[:alnum:]`, `[:alpha:]`, `[:blank:]`, `[:cntrl:]`, `[:digit:]`, `[:graph:]`, `[:lower:]`, `[:print:]`, `[:punct:]`, `[:space:]`, `[:upper:]`, `[:xdigit:]`.
- `gawk`‑specific: `\y` word boundary, `\B` non‑boundary, `\<` start of word, `\>` end of word, `\s` whitespace, `\S` non‑whitespace, `\w` word char, `\W` non‑word char, `` \` `` start of buffer, `\'` end of buffer.
- Collating symbols `[. .]` and equivalence classes `[= =]` are not yet supported.

**Special file names for I/O**:
- `-`, `/dev/stdin`, `/dev/stdout`, `/dev/stderr`, `/dev/fd/n` — standard I/O and file descriptors.
- For TCP/UDP connections with `|&`:
  - `/inet/tcp/lport/rhost/rport`, `/inet4/tcp/…`, `/inet6/tcp/…`
  - `/inet/udp/lport/rhost/rport`, `/inet4/udp/…`, `/inet6/udp/…`

**Redirection**:
- `> file`, `>> file`, `| command`, `|& command` (coprocess/socket).
- `getline` variants: `getline`, `getline < file`, `getline var`, `getline var < file`, `command | getline [var]`, `command |& getline [var]`.
- `close(file [, "to"|"from"])` — close file, pipe, or coprocess.
- `fflush([file])` — flush buffers.
- `system(cmd)` — execute command, return exit status.

## Options
- `-f, --file program-file` — Read AWK program from file. Multiple `-f` allowed.
- `-F, --field-separator fs` — Set input field separator (`FS`).
- `-v, --assign var=val` — Assign variable before execution; available in `BEGIN`.
- `-b, --characters-as-bytes` — Treat all input as single‑byte characters.
- `-c, --traditional` — Compatibility mode (no GNU extensions).
- `-C, --copyright` — Print copyright and exit.
- `-d, --dump-variables[=file]` — Dump global variables to `file` (default `awkvars.out`).
- `-D, --debug[=file]` — Enable debugger; optional command file.
- `-e, --source program-text` — Use `program-text` as AWK source; can be mixed with `-f`.
- `-E, --exec file` — Like `-f` but last option processed; disables command‑line variable assignments.
- `-g, --gen-pot` — Generate `.pot` file for i18n; program not executed.
- `-h, --help` — Print short help and exit.
- `-i, --include file` — Include awk source library; searched via `AWKPATH`.
- `-l, --load lib` — Load shared library extension; searched via `AWKLIBPATH`.
- `-L, --lint[=value]` — Warn about dubious constructs; `fatal` makes errors, `invalid` only invalid, `no-ext` suppresses gawk‑extension warnings.
- `-M, --bignum` — Force arbitrary‑precision arithmetic (requires MPFR/GMP).
- `-n, --non-decimal-data` — Recognize octal/hex values in input data (use with caution).
- `-N, --use-lc-numeric` — Use locale’s decimal point when parsing input data.
- `-o, --pretty-print[=file]` — Pretty‑print program to `file` (default `awkprof.out`); implies `--no-optimize`.
- `-O, --optimize` — Enable default optimizations (on by default).
- `-p, --profile[=file]` — Start profiling session; data to `file` (default `awkprof.out`); implies `--no-optimize`.
- `-P, --posix` — Strict POSIX mode (disables `\x`, line continuation after `?`/`:`, `func` synonym, `**`/`**=` operators).
- `-r, --re-interval` — Enable interval expressions in regexps (default on; useful with `--traditional`).
- `-s, --no-optimize` — Disable optimizations.
- `-S, --sandbox` — Disable `system()`, `getline` redirections, `print`/`printf` redirections, and dynamic extensions.
- `-t, --lint-old` — Warn about constructs not portable to old UNIX awk.
- `-V, --version` — Print version and exit.
- `--` — End of options; remaining arguments treated as AWK program arguments.
- `-W` can be used for POSIX compatibility, followed by long option name.

## Examples
Print and sort login names of all users:
awk
BEGIN { FS = ":" }
      { print $1 | "sort" }
Count lines in a file:
awk
      { nlines++ }
END   { print nlines }
Precede each line by its number in the file:
awk
{ print FNR, $0 }
Concatenate and line number:
awk
{ print NR, $0 }
Run an external command for particular lines of data:
shell
tail -f access_log |
awk '/myhome.html/ { system("nmap " $1 ">> logdir/myhome.html") }'
## See Also
- [egrep(1)](https://www.chedong.com/phpMan.php/man/egrep/1/markdown)
- [sed(1)](https://www.chedong.com/phpMan.php/man/sed/1/markdown)
- [printf(3)](https://www.chedong.com/phpMan.php/man/printf/3/markdown)
- [strftime(3)](https://www.chedong.com/phpMan.php/man/strftime/3/markdown)
- [getpid(2)](https://www.chedong.com/phpMan.php/man/getpid/2/markdown), [getppid(2)](https://www.chedong.com/phpMan.php/man/getppid/2/markdown), [getuid(2)](https://www.chedong.com/phpMan.php/man/getuid/2/markdown), [geteuid(2)](https://www.chedong.com/phpMan.php/man/geteuid/2/markdown), [getgid(2)](https://www.chedong.com/phpMan.php/man/getgid/2/markdown), [getegid(2)](https://www.chedong.com/phpMan.php/man/getegid/2/markdown), [getgroups(2)](https://www.chedong.com/phpMan.php/man/getgroups/2/markdown)
- *The AWK Programming Language* by Aho, Kernighan, Weinberger (Addison‑Wesley, 1988)
- *GAWK: Effective AWK Programming* (online at <https://www.gnu.org/software/gawk/manual>)
- GNU `gettext` documentation (<https://www.gnu.org/software/gettext>)

## Exit Codes
- `0` — success.
- `1` — general error.
- `2` — fatal error (e.g., unopenable file).
If `exit` statement is used with a value, that numeric value is used.