awk Cheatsheet
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.
| Command | Description |
|---|---|
awk '{print}' file.txt | Print all lines |
awk '{print $1}' file.txt | Print first field |
awk '{print $1, $3}' file.txt | Print multiple fields |
awk 'NR==1{print}' file.txt | Print first line |
awk 'NR>1{print}' file.txt | Skip header |
Field Separators
Change the input field separator.
| Command | Description |
|---|---|
awk -F ':' '{print $1}' /etc/passwd | Use colon separator |
awk -F ',' '{print $2}' file.csv | CSV column |
awk -F '\t' '{print $1}' file.tsv | TSV column |
awk 'BEGIN{FS="|"} {print $2}' file.txt | Set FS in script |
Pattern Matching
Filter lines by conditions.
| Command | Description |
|---|---|
awk '/error/ {print}' file.log | Match regex |
awk '$3 > 100 {print}' file.txt | Numeric condition |
awk '$1 == "root" {print}' /etc/passwd | String match |
awk 'NF == 3 {print}' file.txt | Exact field count |
awk 'NF > 0 {print}' file.txt | Non-empty lines |
Calculations
Do arithmetic and totals.
| Command | Description |
|---|---|
awk '{sum += $2} END {print sum}' file.txt | Sum column |
awk '{sum += $2} END {print sum/NR}' file.txt | Average column |
awk 'BEGIN{print 5*7}' | Simple calculation |
awk '$2 > 0 {print $1, $2*1.2}' file.txt | Multiply column |
Output Formatting
Format output and columns.
| Command | Description |
|---|---|
awk '{printf "%s\t%s\n", $1, $2}' file.txt | Tab-separated output |
awk '{printf "%-20s %s\n", $1, $2}' file.txt | Left-align columns |
awk '{printf "%.2f\n", $1}' file.txt | Format numbers |
awk 'BEGIN{OFS=","} {print $1,$2}' file.txt | Set output separator |
Common Options
Useful flags to remember.
| Option | Description |
|---|---|
-F | Set input field separator |
-v | Set variable (e.g., -v limit=10) |
-f | Read program from file |
-E | Use extended regex (gawk) |
--posix | POSIX mode |
Built-in Variables
Variables set automatically by awk.
| Variable | Description |
|---|---|
NR | Current record number (line across all files) |
NF | Number of fields in current record |
FNR | Record number in current file (resets per file) |
FS | Input field separator (default: whitespace) |
RS | Input record separator (default: newline) |
OFS | Output field separator (default: space) |
ORS | Output record separator (default: newline) |
FILENAME | Name of current input file |
$0 | Entire current record |
$NF | Last field in current record |
String Functions
Built-in functions for text manipulation.
| Function | Description |
|---|---|
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).
| Syntax | Description |
|---|---|
arr[key] = val | Assign value to array element |
arr[key] | Access element by key |
for (k in arr) { ... } | Iterate over all keys |
key in arr | Test if key exists |
delete arr[key] | Remove element |
delete arr | Delete entire array |
Control Flow
Flow control statements inside actions.
| Syntax | Description |
|---|---|
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 |
next | Skip to next record |
exit | Stop processing, run END block |