D

AWK

AWK patterns for field extraction, filtering, CSV parsing and log analysis.

Updated 2026-09-03

On this page

AWK reads input line by line, automatically splitting each line into fields ($1, $2, …), and runs pattern { action } blocks against every line. It's the right tool anywhere you'd otherwise reach for a small script to slice columnar text.

Fields & Separators

awk '{print $1}' file

Prints the first whitespace-separated field of every line.

awk '{print $1, $3}' file

Prints the first and third fields, space-separated in the output.

awk -F',' '{print $2}' file.csv

Sets the field separator to a comma, then prints the second field — the basic CSV column extraction pattern.

awk '{print NF}' file

Prints the number of fields on each line — useful for finding malformed rows in otherwise-uniform data.

awk '{print NR, $0}' file

Prefixes every line with its line number (NR) and prints the whole line ($0).

Filtering with Conditions

awk '$3 > 80 {print $1, $3}' file

Prints the first and third fields only for lines where the third field is greater than 80.

awk '/ERROR/ {print}' app.log

Prints every line matching a regex pattern — equivalent to grep, but composable with field logic in the same script.

awk '$1 == "prod"' inventory.txt

Prints lines where the first field exactly equals a string.

awk 'NR > 1' file.csv

Skips the first line — the standard way to drop a CSV header before processing.

BEGIN and END

awk 'BEGIN {print "Report:"} {print $1} END {print "Done"}' file

BEGIN runs once before any input is read; END runs once after the last line — for headers, footers, and final summaries.

Calculations

awk '{sum += $1} END {print sum}' file

Accumulates a running total across every line, printing it once at the end.

awk '{sum += $2; n++} END {print sum/n}' file

Computes an average by accumulating both a sum and a count, then dividing in END.

awk '{if ($3 > max) max = $3} END {print max}' file

Tracks the maximum value seen in a field across all lines.

Variables

awk -v threshold=90 '$2 > threshold {print $1}' file

Passes a shell value into AWK as a variable with -v, avoiding fragile string interpolation into the script itself.

CSV Parsing

awk -F',' 'NR > 1 {print $1, $4}' users.csv

Skips the header row, then prints two columns from a comma-separated file.

awk -F',' '{OFS=","} {$2="REDACTED"; print}' users.csv

Rewrites one field before printing, keeping the comma output separator explicit via OFS.

Naive CSV splitting

Plain -F',' breaks on any field containing a comma inside quotes ("Smith, John"). It's fine for simple, machine-generated CSVs — for anything hand-edited or exported from a spreadsheet, use a real CSV parser instead.

Log Analysis

awk '{print $1}' access.log | sort | uniq -c | sort -rn | head

Counts occurrences of the first field (often an IP or status code in access logs) and shows the most frequent.

awk '$9 >= 500 {print}' access.log

Filters an Apache/nginx-style combined log to only 5xx responses, assuming the status code is the 9th field.

awk '{print $(NF-1), $NF}' file

Prints the second-to-last and last fields — useful when a line's field count varies but the fields you want are always at the end.

Official documentation

Related