awk Basics: Filter and Summarize Logs and CSV Files on Linux
Learn awk by example: pull fields from log lines, filter rows by condition, and total or count columns in logs and simple CSV files with short commands.
Contents
awk reads text one line at a time, splits each line into fields, and runs a small program on it. This guide shows how to pull fields out of a log, filter lines by a condition, and total or count values in a log and a simple CSV file. Each example uses features described in the POSIX awk specification.
How an awk program is built
A program is a list of pattern { action } pairs. For each input line, awk checks every pattern in order and runs the action when the pattern matches. A missing pattern matches every line, and a missing action prints the line.
Inside an action, $0 is the whole line, $1, $2 and so on are its fields, and NF is the number of fields. By default, fields are separated by runs of blanks. BEGIN actions run before any input is read and END actions run after the last line, which makes END the place for totals.
Create a small sample log and a CSV file to work with:
mkdir -p /tmp/awkdemo
cat > /tmp/awkdemo/access.log <<'LOG'
10.0.0.1 GET /index.html 200 512
10.0.0.2 GET /missing 404 128
10.0.0.1 POST /login 302 64
10.0.0.3 GET /index.html 200 512
10.0.0.2 GET /admin 500 256
LOG
cat > /tmp/awkdemo/sales.csv <<'CSV'
date,region,product,amount
2026-10-01,north,widget,19.50
2026-10-01,south,gadget,5.25
2026-10-02,north,gadget,5.25
2026-10-03,south,widget,39.00
CSVPull out fields
Print the client address and the request path, the first and third fields:
awk '{ print $1, $3 }' /tmp/awkdemo/access.log10.0.0.1 /index.html
10.0.0.2 /missing
10.0.0.1 /login
10.0.0.3 /index.html
10.0.0.2 /adminprint joins its arguments with a space. To print the last field regardless of how many there are, use $NF.
Filter lines
A pattern can be a comparison. This prints every line whose fourth field is 400 or higher, meaning an HTTP error status in this sample:
awk '$4 >= 400' /tmp/awkdemo/access.log10.0.0.2 GET /missing 404 128
10.0.0.2 GET /admin 500 256You can combine conditions with && and ||, or match a regular expression against one field with ~:
awk '$2 == "GET" && $3 ~ /index/ { print $1 }' /tmp/awkdemo/access.log10.0.0.1
10.0.0.3Total and count with END
Add the bytes column (the fifth field) and report the sum after the last line:
awk '{ sum += $5 } END { print "bytes:", sum, "lines:", NR }' /tmp/awkdemo/access.logbytes: 1472 lines: 5Variables start out empty, which awk treats as zero in arithmetic, so no initialization is needed. NR is the number of the current line, and in END it holds the total line count.
Count per key with arrays
Awk arrays are associative: the subscript can be any string. Count requests per client address:
awk '{ hits[$1]++ } END { for (ip in hits) print ip, hits[ip] }' /tmp/awkdemo/access.log | sort10.0.0.1 2
10.0.0.2 2
10.0.0.3 1The POSIX specification leaves the order of for (x in array) unspecified, so pipe the result to sort when you need stable output.
Work with CSV files
Set the field separator with -F. A single character other than a space makes each occurrence a delimiter. Print the region and amount, skipping the header with NR > 1:
awk -F, 'NR > 1 { print $2, $4 }' /tmp/awkdemo/sales.csvnorth 19.50
south 5.25
north 5.25
south 39.00Total the amount per region and format the numbers with printf:
awk -F, 'NR > 1 { total[$2] += $4 } END { for (r in total) printf "%s %.2f\n", r, total[r] }' /tmp/awkdemo/sales.csv | sortnorth 24.75
south 44.25print ends its output with a newline by default, while printf writes what the format says, so the format ends with \n.
Limits to know about
Splitting on a comma is naive: with -F, every comma is a delimiter, so a quoted CSV field that contains a comma, such as "Smith, Jane", is cut in two. Use awk for simple, predictable CSV like the sample above, and reach for a real CSV parser when quoting appears.
Also put the awk program in single quotes so the shell does not expand $1 and friends before awk sees them.
Awk also pairs well with other command output that has one record per line. For example, the columns printed by ss can be picked apart the same way, and df, du and ncdu produce tables you can total.
Sources
This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.