AWK Reference

Essential AWK commands, built-in variables, and common patterns.

  • JSON5,220 B
  • YAML3,715 B · −29%
  • TOON2,970 B · −43%

AWK one-liners and patterns for working with columns and records: printing fields, setting the field separator, built-in variables, conditionals, sums, pattern matching and functions, extended with last-field printing, de-duplication, line ranges, averages, maxima and substitutions.

Use it when

  • You need a one-line column or record transformation on text or CSV-like data.
  • You need the idiom for a common AWK task.

Watch out for

  • AWK implementations differ (GNU awk, mawk, BSD awk); gawk-specific features are not portable.
  • For quoted CSV with embedded commas, use a proper CSV tool; awk -F, splits naively.

The data

PatternCommandDescription
Print columnsawk '{print $1, $3}'Print the 1st and 3rd fields of each line.
Field separatorawk -F: '{print $1}' /etc/passwdUse colon as delimiter; print 1st field.
Built-in variablesNR, NF, FS, OFS, RS, ORS, FILENAMENR=record number, NF=field count, FS=OFS=field sep, RS=record sep.
Conditional printawk '$3 > 100 {print $1}'Print $1 only when $3 exceeds 100.
Sum a columnawk '{sum += $2} END {print sum}'Accumulate sum of column 2 and print at END.
Pattern matchingawk '/error/ {print}' log.txtPrint lines matching the regex /error/.
Functionslength(), sub(), gsub(), split(), systime()Built-in string and time functions.
Multi-character FSawk -v FS="\t" '{print $2}'Set field separator to tab using -v flag.
Print last fieldawk '{print $NF}' fileNF is the number of fields, so $NF is the last one.
Remove duplicate linesawk '!seen[$0]++' filePrints each line only the first time it appears, preserving order.
Print a line rangeawk 'NR==2,NR==5' filePrints lines 2 through 5 using the record-number variable NR.
Average a columnawk '{s+=$2} END {print s/NR}' fileAccumulates column 2 and divides by the number of records at the end.
Maximum of a columnawk 'NR==1 || $3>m {m=$3} END {print m}' fileTracks the largest value seen in column 3.
Substitute textawk '{gsub(/foo/,"bar"); print}' fileReplaces all occurrences of foo with bar in each line.
Count matching linesawk '/error/ {n++} END {print n+0}' fileCounts lines containing 'error'; n+0 prints 0 when none match.
Filter by field lengthawk 'length($1) > 5' filePrints lines whose first field is longer than five characters.

Fields

The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.

SectionFieldTypeExample
datapatternstringPrint columns
datacommandstringawk '{print $1, $3}'
datadescriptionstringPrint the 1st and 3rd fields of each line.

Fetch the same bytes

The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.

curl "https://yjtoon.yunusemrevurgun.com/api/dataset/awk-reference?format=toon"
const res = await fetch(
  "https://yjtoon.yunusemrevurgun.com/static-data/dataset/awk-reference.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • unix
  • awk
  • text-processing
  • scripting

Search the library

Search by a topic, title, or keyword.

Start with a collection

Browse all 205 datasets