OnCallReady

Commands

awk - pattern scanning and processing language

awk [-F SEP] [-v VAR=VAL] 'PROGRAM' [file]...

Options you will use

-F SEP
Input field separator. -F: for /etc/passwd, -F, for CSV.
-v VAR=VAL
Set a variable before the program runs.
'PATTERN {ACTION}'
Run ACTION on every line matching PATTERN. Either may be omitted.
BEGIN / END
Blocks that run before the first line and after the last.
$0 $1 $N NF NR FS OFS
Whole line, fields, field count, record number, separators.
-f PROGFILE
Read the awk program from a file instead of the command line.
-F FS
Use FS as the input field separator: -F: -F','.
-v VAR=VALUE
Set an awk variable before the program runs.

Examples

$ awk -F: '$3 >= 1000 {print $1}' /etc/passwd

real login users - and the surprise: nobody has UID 65534

$ awk '{sum+=$10} END {print sum}' access.log

total bytes served

$ awk '$9 ~ /^5/ {c[$1]++} END {for (ip in c) print c[ip], ip}' access.log | sort -rn

who is causing the 5xx

$ awk '{s+=$NF} END {printf "%.3f\n", s/NR}'

average of the last field

Gotchas

Taught in

Try awk in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.