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.
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 folderPrints 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 folderUses 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 folderAccumulates 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
- 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.
- What does NR mean?
- How do you set comma as the field separator?
Your practice, your pace
Loading saved progress…