awk Commands

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.

Options (30)

ShortcutAction
awk -f program-fileRead the AWK program source from the file program-file, instead of from the first command
awk -F fsUse fs for the input field separator (the value of the FS predefined variable)
awk -v var=valAssign the value val to the variable var, before execution of the program begins
awk -bTreat all input data as single-byte characters
awk -cRun in compatibility mode
awk -CPrint 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-textUse program-text as AWK program source code
awk -E fileSimilar to -f, however, this option is the last one processed
awk -gScan and parse the AWK program, and generate a GNU .pot (Portable Object Template) format
awk -hPrint a relatively short summary of the available options on the standard output
awk -i include-fileLoad an awk source library
awk -IPrint the internal byte code names as they are executed when running the program
awk -kEnable CSV special processing
awk -l libLoad 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 -MForce arbitrary precision arithmetic on numbers
awk -nRecognize octal and hexadecimal values in input data
awk -NForce 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 -OEnable 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 -PThis turns on compatibility mode, and disables a number of common extensions
awk -rEnable the use of interval expressions in regular expression matching
awk -sDisable gawk's default optimizations upon the internal representation of the program
awk -SRun gawk in sandbox mode, disabling the system() function, input redirection with getline
awk -tProvide warnings about constructs that are not portable to the original version of UNIX aw
awk -VPrint version information for this particular copy of gawk on the standard output
awk --Signal the end of options

Built-in Variables (18)

ShortcutAction
ARGVArray of command line arguments
BINMODEOn non-POSIX systems, specifies use of "binary" mode for all file I/O
CONVFMTThe conversion format for numbers, "%.6g", by default
ENVIRONAn array containing the values of the current environment
ERRNOIf a system error occurs either doing a redirection for getline, during a read for getline
FIELDWIDTHSA whitespace-separated list of field widths
FILENAMEThe name of the current input file
FPATA regular expression describing the contents of the fields in a record
FSThe input field separator, a space by default
FUNCTABAn array whose indices and corresponding values are the names of all the user-defined or e
IGNORECASEControls the case-sensitivity of all regular expression and string operations
LINTProvides dynamic control of the --lint option from within an AWK program
PRECThe working precision of arbitrary precision floating-point numbers, 53 by default
PROCINFOThe elements of this array provide access to information about the running AWK program
ROUNDMODEThe rounding mode to use for arbitrary precision arithmetic on numbers, by default "N" (IE
RTThe record terminator
RLENGTHThe length of the string matched by match(); -1 if no match
TEXTDOMAINThe text domain of the AWK program; used to find the localized translations for the progra
📄 View Printable Cheat Sheet — Download as PDF or print · 🧩 Combine with other tools

Related Shortcut Pages

sed Regex (Regular Expressions) Bash / Shell jq (JSON) Python tmux

Search 18,500+ shortcuts across 268 platforms

Explore All Platforms Practice Shortcuts
📜 Every shortcut on this page is taken from the official awk documentation — man7.org
🔧 Spotted an error or a missing shortcut? Suggest an edit on GitHub — every accepted fix goes live on this page, the API and the CLI.

Fields, separators and the two options that matter

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.

Variables you read inside the program

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.

CSV and gawk extensions

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.

Frequently asked questions

How do I use a shell variable inside an awk program?

Pass it with -v: awk -v name="$NAME" '$1 == name' file. Do not interpolate it into the quoted program.

How do I print a column with a different separator?

Set OFS: awk -F: -v OFS="\t" '{print $1, $3}' /etc/passwd. The comma in print uses OFS between fields.

How do I handle CSV with quoted commas?

Use gawk's -k option (5.3 and later) or set FPAT to a pattern for quoted fields. Plain -F, splits inside quotes.

What is the difference between NR and FNR?

NR counts records across all input files; FNR resets for each file. NR==FNR is true only while reading the first file.

🤖 Ask AI about awk shortcuts

Open your assistant with this page preloaded as the source — great for follow-up questions like "which of these work in other apps?"

ChatGPT Claude Perplexity Gemini Grok