shell · difficulty ◆◆
awk — pattern scanning and text processing
The one-liner language that turns columns into answers.
You have watched someone type `awk -F, '{print $1, $3}'` and assumed it was wizardry. It is not magic — it is a whole miniature programming language hiding inside a single command, and it has been there since 1977.
AI-generated draft · reviewed and editedThis text was generated with AI as a first draft, then reviewed and edited by the author, who holds editorial responsibility for it.$ awkWhat it does
awk reads input line by line, splits each line into fields (by whitespace, or by any delimiter you set with -F), and runs a program against it. The program is a list of pattern { action } pairs: when the pattern matches a line, the action runs. No pattern means every line; no action means print. That tiny grammar — pattern, action, fields $1 $2 $3, and the special variables NR (line number) and NF (field count) — is enough to do real work: sum a column, filter rows, reformat output, count groups.
Why it matters
awk is the fastest way to answer 'what is the average of column 3?' or 'which rows have a value over 80000?' without opening a spreadsheet or writing a Python script. It is the middle ground between grep (which only finds lines) and a full language. Where grep stops at matching, awk computes. It is guaranteed present on every Unix-like system, and its END block lets you summarize an entire file after the last line — the pattern that turns a log dump into a report.
Numbers, strings, and the comparison that surprises people
awk's fields are numeric strings: a field that looks like a number compares numerically, but two constants in quotes compare as text. `echo "10 9" | awk '{print ($1 > $2)}'` prints 1, while `awk 'BEGIN{print ("10" > "9")}'` prints 0 — the same characters, compared as words. Two habits are worth forming. First, parenthesise: inside a print statement `>` is redirection, not comparison, so `print $1 > $2` sends awk looking for a file named by the second field and it reports a redirection with a null string value. Second, know what an unset variable holds: uninitialised variables are the empty string and the number 0, so arithmetic on a misspelled name quietly yields zero instead of an error.
Example
$ awk -F, '{print $1, $3}' employees.csvname salary
alice 95000
bob 72000
carol 88000
dave 65000
eve 101000-F, sets the field separator to a comma. $1 and $3 are the first and third fields of each line.
$ awk -F, 'NR>1 {sum+=$3} END {print "avg:", sum/(NR-1)}' employees.csvavg: 84200NR is the line number. NR>1 skips the header, the END block runs once after the last line, and (NR-1) is the data-row count.
$ awk -F, '$2=="eng" {print $1}' employees.csvalice
carol
eveA pattern that filters: only lines whose second field equals eng trigger the print.
$ awk -F, 'NR>1 && $3>80000 {print $1, $3}' employees.csvalice 95000
carol 88000
eve 101000Combine conditions with &&. This is grep-plus-math: filter by a numeric threshold on one field.
$ awk -F, 'NR>1 {print $1, $3*1.1}' employees.csvalice 104500
bob 79200
carol 96800
dave 71500
eve 111100awk does arithmetic on fields. A 10% raise, computed across the whole file in one pass.
$ awk -F, 'NR>1 {a[$2]++} END {for (d in a) print d, a[d]}' employees.csveng 3
sales 2Associative arrays count occurrences per group. This is a GROUP BY in one line — the classic awk party trick.
$ seq 5 | awk 'NR==2, NR==4'2
3
4A range pattern prints from the line where the first condition becomes true to the line where the second one does — the V7 manual used exactly this to demonstrate 'print all lines between start/stop pairs'. Both ends are inclusive, and either side can be a pattern (/start/,/stop/) rather than a line number.
Common flags
- -F,
- set the field separator (here a comma); default is whitespace
- -v var=val
- assign a variable before the program runs, e.g. -v threshold=80000
- -f file
- read the awk program from a file instead of the command line
- NR
- built-in: the current line number (1-based)
- NF
- built-in: the number of fields in the current line
- $0
- the whole current line; $1, $2, ... are the individual fields
- END { }
- block that runs once after the last line — for summaries
History
Origin
awk was created in 1977 at Bell Labs by Alfred Aho, Peter Weinberger, and Brian Kernighan — the initials A, W, and K are the name. It was designed as a data-driven language for text processing, a step up from grep that could compute as well as match, and the first version shipped with Version 7 Unix in 1979. The original was a small implementation built with lex and yacc; the modern GNU version (gawk) added user-defined functions, multiple input streams, computed regular expressions and a debugger, which made it a full programming language.
The name that stuck
The three authors' surnames — Aho, Weinberger, Kernighan — are the whole story of the name, and the V7 manual cites their joint 1977 report, 'Awk — a pattern scanning and processing language', as the reference. The language outlived its original purpose and became a general text-processing tool, and it is one of the things Perl was written to subsume: the perl(1) manual says Perl 'combines (in the author's opinion, anyway) some of the best features of C, sed, awk, and sh'. Perl's awk inheritance is clearest in its -a and -F switches, which autosplit each input line into the array @F.
Fun facts
Pros & cons
pros
- + One command does filtering, arithmetic, and grouping — no script file needed
- + Required by POSIX and the Single UNIX Specification, and part of Unix since Version 7 (1979) — every conforming box has it
- + The END block makes whole-file summaries trivial
- + Associative arrays give you GROUP BY in a single line
cons
- − The pattern { action } syntax is terse and easy to misread at a glance
- − Field splitting on whitespace surprises people with delimited data
- − For anything beyond a few lines, a real script (Python, Perl) is more readable
- − GNU awk and BSD awk differ subtly in some edge behaviors
Takeaways
- 1Split on a delimiter with -F, and reference fields as $1, $2, $3 — $0 is the whole line
- 2Sum a column and skip the header: `awk -F, 'NR>1 {sum+=$3} END {print sum}' file`
- 3Filter with a pattern: `awk -F, '$3>80000 {print $1}' file`
- 4Group and count with arrays: `awk '{a[$1]++} END {for (k in a) print k, a[k]}' file`
- 5Use -v to pass variables in, and reach for a real script once the logic outgrows a line
