← All 105 books awk, one lesson per page
One lesson per page

awk, one lesson per page

Thirty lessons for the pattern scanning and processing language already on every Unix: patterns and actions, fields and separators, BEGIN and END, associative arrays, printf columns, and the classic report and pivot one-liners.


Steve Hodgkiss 6 lessons

A diagram, the map, and one thing to try this week. That's a page.

awk, one lesson per page

Thirty lessons for the pattern scanning and processing language already on every Unix: patterns and actions, fields and separators, BEGIN and END, associative arrays, printf columns, and the classic report and pivot one-liners.


Set in Space Grotesk, Inter and JetBrains Mono (SIL Open Font License).

Every fact in this book is as the official source states it, fetched and read during this build: The GNU Awk User's Guide at gnu.org/software/gawk/manual (Edition 5.4), cross-checked against the local system gawk(1) manual (GNU Awk 5.2.1) and, for the version-gated page, the local mawk(1) manual; behaviour noted as differing across awk implementations is flagged on the page. Demand evidence from live command-line threads, reconfirmed at dispatch; no facts are sourced from tutorials, howtogeek, linuxize, Reddit or StackExchange. An independent guide, not affiliated with or endorsed by the Free Software Foundation or the GNU project.

Your purchase is for personal use only. You do not have redistribution rights: please do not share, resell, or republish this book or its pages.

© 2026 Steve Hodgkiss. All rights reserved. Personal use only; no redistribution rights.

Edition 1.0 · stevehodgkiss.net

Contents

Contents


Part 1 · The rule
Pattern, then action4
Part 2 · Fields
Fields by number5
Part 3 · Patterns6
/slash/ matches7
Before the input8
Part 4 · Arrays
Count with arrays9
Part 5 · Habits
One habit to keep10

Per The GNU Awk User's Guide: 'Each rule specifies one pattern to search for and one action to perform upon finding the pattern. Syntactically, a rule consists of a pattern followed by an action. The action is enclosed in braces to separate it from the pattern.'

pattern scanning · No. 01
The rule

Pattern, then action

The whole language in one line

the shape every awk program sharesone rule$/error/ { print $1 }/error/{ print $1 }the patternthe actionthe pattern pickswhich records firethe action doesthe work on themeither half can go(lesson three)one question, one answer: that is a whole awk program

Every awk program is rules, and every rule is two halves. Per the guide, a rule is a pattern followed by an action, the action wrapped in braces. The pattern chooses the lines; the action does the work.

The guide introduces awk as a program to select particular records in a file and perform operations upon them. Select, then operate: that is the whole shape.

Read any awk one-liner as a question and its answer. The question is the pattern; the answer is the action.

TRY IT THIS WEEK

Write one rule this week, /error/ { print $1 }, and read it aloud as question then answer before you run it.

Per The GNU Awk User's Guide: 'Here the first field, or $1, is This, the second field, or $2, is seems, and so on'; each line is one record, and awk splits every record into fields as it reads.

pattern scanning · No. 02
Fields

Fields by number

Columns without a parser

every line arrives already splitone record:Beth 4.00 0already cut into fields$1 = Beth, $2 = 4.00, $3 = 0whitespace splits by default$1 is the first field,$2 the second, and so onlogs, ls output, csv:column text is a tablethe moment awk reads itthe dollars are positions: field two is always $2

awk reads a line and cuts it up before your program sees it. Per the guide, the first field is $1, the second $2, and so on: no split call, no index arithmetic.

Whitespace separates fields by default, so any column-shaped text is already a database: logs, ls output, csv once you set the separator.

The dollars are the reason awk won the column jobs. Field two of every line is $2, always.

TRY IT THIS WEEK

Print field one of the password file this week: awk -F : '{ print $1 }' /etc/passwd.

Part 3 of 5
when the action fires
3

Patterns

The /regular expression/ pattern, any expression as a pattern such as $3 greater than 0, the begpat, endpat range, and BEGIN for work before any input.


In this part
  1. 01/slash/ matches
  2. 02Before the input

Per The GNU Awk User's Guide, kinds of patterns: '/regular expression/: A regular expression. It matches when the text of the input record fits the regular expression.'

pattern scanning · No. 03
Patterns

/slash/ matches

The grep you write inline

slashes test the whole record17:02 ok job 447117:04 error timeout 447317:05 ok job 4474the pattern tests the textof the whole record/error/matchesline two{ print NR, $0 }slash question, brace answergrep's question, asked inside a rule

The most familiar pattern wears slashes. Per the guide, /regular expression/ matches when the text of the input record fits the regular expression.

It tests the whole record, like grep, but it is one half of a rule: pair it with an action and it selects which lines the action works on.

Slashes select, braces act. Every other pattern is this one asking a different question.

TRY IT THIS WEEK

Write /error/ { print NR, $0 } this week and notice the regex and the action working as one rule.

Per The GNU Awk User's Guide: 'BEGIN and END rules are each executed exactly once, at the beginning and end of your awk program, respectively.'

pattern scanning · No. 04
Patterns

Before the input

BEGIN runs once, first

BEGIN: once, before the first recordBEGINruns exactly once,before any inputheaders live herethe recordslineline...ENDafter the last(lesson sixteen)OFSseedsawk 'BEGIN { ... }' runs with no files at allset the table before the data arrives

Some work belongs before any data: headers, OFS, seeded variables. Per the guide, BEGIN rules are each executed exactly once, at the beginning of the program.

A BEGIN-only program runs with no input files at all, which makes awk 'BEGIN { ... }' a scrappy calculator and a printf test bench.

BEGIN is where the report sets its table: titles printed, separators chosen, counters named.

TRY IT THIS WEEK

Print a header before your data this week: BEGIN { print "user uid" } then the matching rows.

Per The GNU Awk User's Guide: awk's arrays are associative, and 'one advantage of associative arrays is that new pairs can be added at any time'; the guide's word-usage program builds counts under keys as they appear.

pattern scanning · No. 05
Arrays

Count with arrays

count[$1]++ is the idiom

count[$1]++ builds the tally as it readsinput, unsorted:GET /aPOST /bGET /athe second /a is notnew: it just countsc["/a"] =2c["/b"] =1keys born on first write,no declaration, no sizing:keys are strings, any valueuniq -c without the sort

Every awk array is an associative array: keys, not positions. Per the guide, new pairs can be added at any time, born when first written.

So counting is one expression: count[$1]++. First sighting creates the entry and increments it; later sightings just increment. No declaration, no sizing.

uniq -c without the sort. This one idiom is half of why arrays exist in awk.

TRY IT THIS WEEK

Count by first field this week: awk '{ c[$1]++ } END { for (k in c) print c[k], k }' on unsorted data.

This book's plan: one rule a week, kept: find a column-shaped chore, write one pattern and one action for it, run it on the real data, and keep the one-liners that survive in a notes file.

pattern scanning · No. 06
Habits

One habit to keep

One rule a week

one rule a week, keptfind a column chorewrite one rulerun on real datakeep itone-linersthe survivors, with thecommand that ran themand the datethe dedupe, the header skip,the column sum, the gatekeep a weekly rule and the fluency stays

The skill compounds like the counters do. One real chore a week: pick the file, write one rule, run it against the actual data until it earns its place.

Keep the survivors in a file of one-liners. The dedupe, the header skip, the column sum, the version gate: four rules already carry most reporting work.

You now read the pattern-action shape the way you read a sentence. Keep a weekly rule and the fluency stays.

TRY IT THIS WEEK

Ship one rule this week into your notes file, with the command that ran it and the date, and start the collection.

Index

Index


/slash/ matches7
Before the input8
Count with arrays9
Fields by number5
One habit to keep10
Pattern, then action4