text

awk

Pattern-action language for processing columnar text. Excellent for reshaping tabular data, aggregating fields, and quick reports.

awk [OPTION]... 'program' [FILE]...

Common flags

Flag Purpose
-F Set the input field separator
-v Assign an awk variable from the shell (e.g. -v key=value)
-f Read the awk program from a file
-e Append a program-text fragment (GNU awk)

Examples

awk '{print $1, $NF}' access.log

Print the first and last whitespace-separated fields per line

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

List non-system usernames using : as the separator

awk '{sum += $2} END {print sum}' sales.tsv

Sum the second column

ps -eo rss,cmd | awk '{m[$2]+=$1} END{for(c in m) print m[c], c}'

Aggregate RSS by command

Gotcha

Fields default to whitespace-splitting; if your data has empty fields, use -F with an explicit delimiter and check FS/OFS semantics.

Related commands

← All Linux commands · Linux cheat sheet