Introduction
AWK is a programming language built around one specific idea: process text line by line, splitting each line into fields automatically, and run an action whenever a pattern matches. Where SED (the companion course to this one) excels at find-and-replace text transformation, AWK excels at anything involving structured, column-based data — log files, CSVs, command output — anywhere "this is field 3 of this line" is a meaningful thing to say.
The Pattern-Action Structure
Every AWK program, no matter how complex, is built from the same fundamental unit: a pattern paired with an action. AWK reads input one line at a time, checks the pattern against the current line, and runs the action if it matches.
$ awk '/ERROR/ { print }' app.log
Either half can be omitted, with a specific meaning each time:
grep for that pattern.$ awk '/ERROR/' app.log
$ # Just an action — runs for every single line, no filtering
$ awk '{ print "Line: " $0 }' app.log
Your First Real Programs
$ awk '{ print }' file.txt
$ # Print only lines longer than 80 characters
$ awk 'length > 80' file.txt
$ # Print lines matching a regex pattern (more on this in Chapter 3)
$ awk '/^#/ { print "comment: " $0 }' config.conf
Multiple Pattern-Action Pairs
A single AWK program can have several pattern-action pairs — each line of input is checked against every pattern in order, and every matching action runs (not just the first match).
/ERROR/ { print "Found error: " $0 }
/WARN/ { print "Found warning: " $0 }
' app.log
AWK vs grep vs sed — When to Reach for Each
grep to narrow down which lines matter, pipe into awk to extract specific fields or compute something, and finish with sed for a final text cleanup — three small, composable tools rather than one tool trying to do everything. Chapter 8 of this course returns to exactly this kind of combined pipeline.
Running AWK — Three Ways
$ awk '{ print $1 }' file.txt
$ # From a script file, with -f
$ awk -f myscript.awk file.txt
$ # Reading from a pipe instead of a file
$ ps aux | awk '{ print $1 }'
$1, $2, etc. for field references (Chapter 2) — inside double quotes, the shell would try to expand those as its own variables before AWK ever sees them. Single quotes prevent any shell expansion, letting AWK interpret the program exactly as written.
Command Reference
| Form | What it does |
|---|---|
| awk 'pattern { action }' file | Run action on lines matching pattern |
| awk '{ action }' file | Run action on every line, no filtering |
| awk 'pattern' file | Print every matching line (default action) |
| awk -f script.awk file | Run a program from a script file |
Chapter 1 Quick Reference
- pattern { action } — the fundamental unit of every AWK program
- No pattern = runs on every line; no action = defaults to printing the matched line
- Multiple pattern-action pairs are checked independently against every line — all matches run, not just the first
- AWK vs grep vs sed: grep finds, sed transforms text, AWK does structured field processing with real programming constructs
- Always single-quote AWK programs in the shell — prevents the shell from expanding $1, $2, etc. itself
- Next chapter: fields and records — $0, $1, NF, NR, and field separators