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}' filePrints the first whitespace-separated field of every line.
awk '{print $1, $3}' filePrints the first and third fields, space-separated in the output.
awk -F',' '{print $2}' file.csvSets the field separator to a comma, then prints the second field — the basic CSV column extraction pattern.
awk '{print NF}' filePrints the number of fields on each line — useful for finding malformed rows in otherwise-uniform data.
awk '{print NR, $0}' filePrefixes every line with its line number (NR) and prints the whole line ($0).
Filtering with Conditions
awk '$3 > 80 {print $1, $3}' filePrints the first and third fields only for lines where the third field is greater than 80.
awk '/ERROR/ {print}' app.logPrints every line matching a regex pattern — equivalent to grep, but composable with field logic in the same script.
awk '$1 == "prod"' inventory.txtPrints lines where the first field exactly equals a string.
awk 'NR > 1' file.csvSkips 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"}' fileBEGIN 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}' fileAccumulates a running total across every line, printing it once at the end.
awk '{sum += $2; n++} END {print sum/n}' fileComputes an average by accumulating both a sum and a count, then dividing in END.
awk '{if ($3 > max) max = $3} END {print max}' fileTracks the maximum value seen in a field across all lines.
Variables
awk -v threshold=90 '$2 > threshold {print $1}' filePasses 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.csvSkips the header row, then prints two columns from a comma-separated file.
awk -F',' '{OFS=","} {$2="REDACTED"; print}' users.csvRewrites 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 | headCounts occurrences of the first field (often an IP or status code in access logs) and shows the most frequent.
awk '$9 >= 500 {print}' access.logFilters an Apache/nginx-style combined log to only 5xx responses, assuming the status code is the 9th field.
awk '{print $(NF-1), $NF}' filePrints 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.