awk command examples: columns, filters and sums
Updated · Every command on this page ran in our practice shell
Short answer
awk '{print $2}' file prints the second space-separated column of every line. Use -F, for CSV files, a condition like NR > 1 or $3 > 100 to pick lines, and awk -F, '{s += $3} END {print s}' file to add up a column.
awk reads text one line at a time and splits each line into fields. That makes it ideal for logs and CSV files: print a column, keep the rows where a value is too high, or total a column. The examples below use a bakery's web log and a small sales CSV.
Print a column
$1 is the first field, $2 the second, and $0 the whole line. By default awk splits on any run of spaces or tabs and ignores leading blanks, which is why it copes with messy spacing that breaks cut -d' '. The program goes in single quotes so the shell doesn't touch the $.
learner@shellstreak:~/bakery$ awk '{print $4}' logs/access.log /index.html /style.css /menu.html /order /index.html /admin /menu.html /order /index.html /about.html
Print several fields
Separate fields with commas in print to get a space between them. $NF is the last field, whatever the number of columns.
learner@shellstreak:~/bakery$ awk '{print $4, $NF}' logs/access.log /index.html 200 /style.css 200 /menu.html 404 /order 200 /index.html 200 /admin 403 /menu.html 404 /order 500 /index.html 200 /about.html 200
Split CSV files with -F
-F, makes the comma the field separator. Without it, a CSV line has no spaces, so the whole line is field 1 and $2 is empty.
learner@shellstreak:~/bakery$ awk -F, '{print $2}' data/sales.csv item sourdough croissant rye sourdough focaccia croissant
Skip the header with NR
NR is the current line number. A condition before the braces picks which lines the action runs on, so NR > 1 skips the header row. awk can also do maths in the action.
learner@shellstreak:~/bakery$ awk -F, 'NR > 1 {print $2, $3 * $4}' data/sales.csv sourdough 24 croissant 36 rye 10 sourdough 36 focaccia 12 croissant 24
Filter rows by value
A condition with no action prints the matching lines. Compare numbers with >, >=, == and friends, or match text with $2 == "rye" or a pattern like /POST/.
learner@shellstreak:~/bakery$ awk '$5 >= 400' logs/access.log 2026-03-02 09:15:22 GET /menu.html 404 2026-03-02 09:18:55 GET /admin 403 2026-03-02 09:19:12 GET /menu.html 404 2026-03-02 09:20:30 POST /order 500
Add up a column with END
Keep a running total in a variable, then print it in an END block, which runs once after the last line. Count lines in the same pass to get an average.
learner@shellstreak:~/bakery$ awk -F, 'NR > 1 {s += $3; n++} END {print s, s / n}' data/sales.csv 35 5.83333
Pass a shell value with -v
Don't paste shell variables into the awk program. -v name=value hands a value to awk as a variable of its own.
learner@shellstreak:~/bakery$ awk -F, -v min=5 'NR > 1 && $3 >= min {print $2, $3}' data/sales.csv croissant 12 sourdough 6 croissant 8
Check yourself
One real exercise from the “Text processing” unit. No account needed.How many items did the bakery sell in total? Add up the qty column (column 3) of data/sales.csv and print just the sum.
What's checked: the output of your last command.
Common mistakes
Writing the program in double quotes. The shell then replaces
$1with its own (usually empty) variable before awk runs.Forgetting
-F,on a CSV file and getting blank output.Including the header row in sums or averages. Skip it with
NR > 1.Expecting
-F,to understand quoted CSV fields like"Lima, Ana". awk splits on every comma; use a CSV-aware tool for those files.
Questions
How do I print the last column with awk?+
Use `$NF`. `NF` is the number of fields on the line, so `$NF` is the last one and `$(NF-1)` the one before it.
Should I use awk or cut?+
`cut -d, -f2` is fine for pulling out a column. Use awk when you also need to filter rows, do maths, or deal with runs of spaces between columns.
How do I sum a column in a CSV file?+
`awk -F, 'NR > 1 {s += $3} END {print s}' file.csv` adds up column 3, skipping the header.
Reading is step one. Now make it stick.
Practise this in a five-minute lesson with instant feedback. No account needed.