Skip to main content

awk Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for the awk text processing command

Awk is a powerful text processing tool for working with columns, patterns, and simple transformations. This cheatsheet covers common awk patterns, fields, and formatting tasks.

Basic Usage

Process lines and fields.

CommandDescription
awk '{print}' file.txtPrint all lines
awk '{print $1}' file.txtPrint first field
awk '{print $1, $3}' file.txtPrint multiple fields
awk 'NR==1{print}' file.txtPrint first line
awk 'NR>1{print}' file.txtSkip header

Field Separators

Change the input field separator.

CommandDescription
awk -F ':' '{print $1}' /etc/passwdUse colon separator
awk -F ',' '{print $2}' file.csvCSV column
awk -F '\t' '{print $1}' file.tsvTSV column
awk 'BEGIN{FS="|"} {print $2}' file.txtSet FS in script

Pattern Matching

Filter lines by conditions.

CommandDescription
awk '/error/ {print}' file.logMatch regex
awk '$3 > 100 {print}' file.txtNumeric condition
awk '$1 == "root" {print}' /etc/passwdString match
awk 'NF == 3 {print}' file.txtExact field count
awk 'NF > 0 {print}' file.txtNon-empty lines

Calculations

Do arithmetic and totals.

CommandDescription
awk '{sum += $2} END {print sum}' file.txtSum column
awk '{sum += $2} END {print sum/NR}' file.txtAverage column
awk 'BEGIN{print 5*7}'Simple calculation
awk '$2 > 0 {print $1, $2*1.2}' file.txtMultiply column

Output Formatting

Format output and columns.

CommandDescription
awk '{printf "%s\t%s\n", $1, $2}' file.txtTab-separated output
awk '{printf "%-20s %s\n", $1, $2}' file.txtLeft-align columns
awk '{printf "%.2f\n", $1}' file.txtFormat numbers
awk 'BEGIN{OFS=","} {print $1,$2}' file.txtSet output separator

Common Options

Useful flags to remember.

OptionDescription
-FSet input field separator
-vSet variable (e.g., -v limit=10)
-fRead program from file
-EUse extended regex (gawk)
--posixPOSIX mode

Built-in Variables

Variables set automatically by awk.

VariableDescription
NRCurrent record number (line across all files)
NFNumber of fields in current record
FNRRecord number in current file (resets per file)
FSInput field separator (default: whitespace)
RSInput record separator (default: newline)
OFSOutput field separator (default: space)
ORSOutput record separator (default: newline)
FILENAMEName of current input file
$0Entire current record
$NFLast field in current record

String Functions

Built-in functions for text manipulation.

FunctionDescription
length(s)Number of characters in s
substr(s, start, n)Substring of s from start, length n
split(s, arr, sep)Split s into array arr; returns element count
sub(regex, repl, s)Replace first match of regex in s
gsub(regex, repl, s)Replace all matches of regex in s
index(s, target)Position of target in s (0 if not found)
match(s, regex)Position of first regex match in s (0 if none)
tolower(s)Convert s to lowercase
toupper(s)Convert s to uppercase

Arrays

Awk arrays are associative (string keys).

SyntaxDescription
arr[key] = valAssign value to array element
arr[key]Access element by key
for (k in arr) { ... }Iterate over all keys
key in arrTest if key exists
delete arr[key]Remove element
delete arrDelete entire array

Control Flow

Flow control statements inside actions.

SyntaxDescription
if (cond) { ... }Conditional block
if (cond) { ... } else { ... }If/else branch
for (i=1; i<=n; i++) { ... }C-style for loop
for (k in arr) { ... }Iterate over array keys
while (cond) { ... }While loop
nextSkip to next record
exitStop processing, run END block