Linux Basics: Filtering and Totaling Fields with awk
awk is useful when you want to select records by a field value or calculate a total. It reads one record at a time, usually one line, and splits that record into fields.
Use Bash on Debian or Ubuntu. Run these examples in order in the same shell:
practice=$(mktemp -d) && cd "$practice"Continue only if that command succeeds. The examples use basic features supported by both mawk and GNU awk.
Make a small data file
printf 'apples 3\npears 5\napples 2\n' > stock.txtEach line contains an item name and a quantity. With the default field separator, runs of spaces or tabs separate fields.
Choose records by name
awk '$1 == "apples" { print $2 }' stock.txt3
2$1 is the first field and $2 is the second. The condition selects apple records; the action inside braces prints their quantities. $0 means the entire record.
Keep the awk program in single quotes so the shell does not expand its dollar signs.
Add quantities
awk '{ total += $2 } END { print total }' stock.txt10The first action runs for each record. END runs after all input has been read. In this numeric calculation, the initially unset total starts as zero.
Combine a condition with the same calculation to total just the apples:
awk '$1 == "apples" { total += $2 } END { print total + 0 }' stock.txt5Adding zero in the final expression also produces a numeric zero if no record matches.
This exercise assumes every quantity is a valid number. awk can convert unexpected text to a number, sometimes zero, so validate real input before trusting a total. Its default field splitting also does not understand quoted CSV fields or item names containing spaces.
Clean up
rm -- stock.txt
cd / && rmdir -- "$practice"
unset practiceReference
Acknowledgements
This post was created with assistance from AI.
Linux Basics — lesson 23 of 25. Series index
Previous: Replacing Text with sed · Next: Understanding File Permission Bits