contentintech
Tools

Awk

Extract fields and summarize text with small, readable programs.

Updated Official documentation ↗

Fields and separators

awk '{print $1}' file.txtPrint the first whitespace-separated field
awk -F, '{print $2}' data.csvSplit simple comma-delimited text; not a full CSV parser
awk '{print NR, $0}' file.txtPrefix each record with its number
awk '{print NF}' file.txtCount fields on each record

Select records

awk '/error/' app.logPrint matching records
awk 'NR > 1' data.txtSkip the first record
awk '$3 > 100' data.txtSelect by a numeric field
awk 'length($0) > 80' file.txtFind long lines

Aggregate

awk '{total += $2} END {print total}' data.txtSum the second field
awk 'END {print NR}' file.txtCount records
awk '{count[$1]++} END {for (k in count) print k, count[k]}' data.txtCount records by first field
awk 'BEGIN {OFS=","} {print $1,$2}' data.txtSet the output field separator

Section navigation