AWK Reference
Essential AWK commands, built-in variables, and common patterns.
Essential AWK commands, built-in variables, and common patterns.
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.
| Pattern | Command | Description |
|---|---|---|
| Print columns | awk '{print $1, $3}' | Print the 1st and 3rd fields of each line. |
| Field separator | awk -F: '{print $1}' /etc/passwd | Use colon as delimiter; print 1st field. |
| Built-in variables | NR, NF, FS, OFS, RS, ORS, FILENAME | NR=record number, NF=field count, FS=OFS=field sep, RS=record sep. |
| Conditional print | awk '$3 > 100 {print $1}' | Print $1 only when $3 exceeds 100. |
| Sum a column | awk '{sum += $2} END {print sum}' | Accumulate sum of column 2 and print at END. |
| Pattern matching | awk '/error/ {print}' log.txt | Print lines matching the regex /error/. |
| Functions | length(), sub(), gsub(), split(), systime() | Built-in string and time functions. |
| Multi-character FS | awk -v FS="\t" '{print $2}' | Set field separator to tab using -v flag. |
| Print last field | awk '{print $NF}' file | NF is the number of fields, so $NF is the last one. |
| Remove duplicate lines | awk '!seen[$0]++' file | Prints each line only the first time it appears, preserving order. |
| Print a line range | awk 'NR==2,NR==5' file | Prints lines 2 through 5 using the record-number variable NR. |
| Average a column | awk '{s+=$2} END {print s/NR}' file | Accumulates column 2 and divides by the number of records at the end. |
| Maximum of a column | awk 'NR==1 || $3>m {m=$3} END {print m}' file | Tracks the largest value seen in column 3. |
| Substitute text | awk '{gsub(/foo/,"bar"); print}' file | Replaces all occurrences of foo with bar in each line. |
| Count matching lines | awk '/error/ {n++} END {print n+0}' file | Counts lines containing 'error'; n+0 prints 0 when none match. |
| Filter by field length | awk 'length($1) > 5' file | Prints lines whose first field is longer than five characters. |
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.
| Section | Field | Type | Example |
|---|---|---|---|
| data | pattern | string | Print columns |
| data | command | string | awk '{print $1, $3}' |
| data | description | string | Print the 1st and 3rd fields of each line. |
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 →