awk

A small programming language for text that splits each line into fields and runs actions on the lines that match a condition.

In more detail

Fields are $1, $2 and so on, a built-in variable counts the lines read so far, and -F sets the field separator, as in awk -F, for comma-separated data. A program is a series of condition { action } pairs, plus an optional block that runs after the last line, which makes totals easy. Unlike cut, its default separator treats any run of spaces as one.

Examples

awk -F, 'NR > 1 && $3 > 100 { print $1, $2, $3 }' sales.csv
awk -F, 'NR > 1 { total += $3 } END { print total }' sales.csv
awk '{print $1}'
awk 'END {print NR}'   # count lines
A one-line awk program can count failed passwords per source address in an auth log: match lines containing "Failed password", pick the field holding the address, and add to a counter.

Related terms

Taught in

References

Certification courses