AWK Reference

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

The data

0

Pattern
Print columns
Command
awk '{print $1, $3}'
Description
Print the 1st and 3rd fields of each line.

1

Pattern
Field separator
Command
awk -F: '{print $1}' /etc/passwd
Description
Use colon as delimiter; print 1st field.

2

Pattern
Built-in variables
Command
NR, NF, FS, OFS, RS, ORS, FILENAME
Description
NR=record number, NF=field count, FS=OFS=field sep, RS=record sep.

3

Pattern
Conditional print
Command
awk '$3 > 100 {print $1}'
Description
Print $1 only when $3 exceeds 100.

4

Pattern
Sum a column
Command
awk '{sum += $2} END {print sum}'
Description
Accumulate sum of column 2 and print at END.

5

Pattern
Pattern matching
Command
awk '/error/ {print}' log.txt
Description
Print lines matching the regex /error/.

6

Pattern
Functions
Command
length(), sub(), gsub(), split(), systime()
Description
Built-in string and time functions.

7

Pattern
Multi-character FS
Command
awk -v FS="\t" '{print $2}'
Description
Set field separator to tab using -v flag.

Fetch the same bytes

The static files are identical to what the API returns, but with no rate limit and no server round trip. Use the API when you want a query and a content type; use the files when you want to cache one document.

curl "https://yjtoon.com/api/dataset/awk-reference?format=toon"
const res = await fetch(
  "https://yjtoon.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