Mission 10 of 30 · Milestone

Awk Fundamentals

Calculate useful totals from a small inventory file.

Use awk to work with columns, records, field separators, conditions, and totals.

45–55 min Week 2 · Search, text, permissions, processes Ubuntu terminal
Your outcome: Tables and CSV-like files become approachable from the terminal.
Practice inside ~/linux-practice. Check your setup before you begin. Prerequisite: Sed: Stream Editing Safely .

What you’ll practice

  • Print fields
  • Use separators
  • Filter and calculate

The mental model

  • awk sees each line as a record and each column as a field.
  • $0 is the whole line; $1 is first field; NF is number of fields; NR is line number.

Try it in your terminal

Print fields

Safe in practice folder

Prints columns and line numbers.

Run from ~/linux-practice

cd ~/linux-practice
echo -e 'Name Age City\nJohn 30 NYC\nJane 25 LA' > data.txt
awk '{print $1, $3}' data.txt
awk '{print NR, $0}' data.txt

CSV separator

Safe in practice folder

Uses comma as a field separator and calculates values.

Run from ~/linux-practice

echo -e 'Product,Price,Quantity\nApple,1.50,100\nBanana,0.75,200' > inventory.csv
awk -F, 'NR>1 {print $1, $2 * $3}' inventory.csv

Total calculation

Safe in practice folder

Accumulates a total and prints it at the end.

Run from ~/linux-practice

awk -F, 'NR>1 {total += $2 * $3} END {print total}' inventory.csv

Put it into practice

  1. Create a small expenses.csv and calculate total spending.

Check your result

The calculated total matches the source data.

Milestone challenge

Build a report from a small CSV: filter the rows you need, transform one value, calculate a total, and save the verified result without changing the source.

Common mistakes

  • You run `awk '{print $2}'` on a CSV file and get the wrong column. That's because awk splits on whitespace by default, not commas. Add `-F,` to tell awk your separator is a comma: `awk -F, '{print $2}' inventory.csv`. Without it, 'Apple,1.50,100' is one field, not three.
  • You calculate a total with awk and the number is wrong by the first row's amount. Your header row — 'Product,Price,Quantity' — got included in the math. Add `NR>1` to skip it: `awk -F, 'NR>1 {total += $2}' file.csv`. NR is the row number, so NR>1 means 'start from row 2.'

Check your understanding

Explain these in your own words. If you’re unsure, revisit the examples before moving on.

  1. What does NR mean?
  2. How do you set comma as the field separator?

Your practice, your pace

Loading saved progress…

Discussion

Continue the conversation about this guide. Loading comments connects to GitHub Discussions through giscus.

Open community discussions on GitHub

Check before you copy

This command needs care. Read its context, confirm the target and location, and keep a recovery path.

Copying does not run a command.