03 / WORKING WITH TEXT · LESSON 12

Field-based processing with awk

Filter structured fields and calculate a numeric report with awk.

13 min · learn at your pace

Think in records and fields

Awk reads records, normally one line at a time, and splits each into fields. $0 is the whole record, $1 the first field, and NF the number of fields. -F '\t' selects a tab separator. These dollar expressions belong to awk; wrap the program in single quotes so Bash leaves them alone.

A rule has a condition and an action: $3 < 20 { print $1 }. A BEGIN action runs before input, and END runs after it. Use printf for an explicit output format and a stable delimiter. Awk performs numeric conversion, but that is not validation: text that begins with digits can still be invalid data.

Keep the format contract visible

This lesson supplies a header and validated numeric capacity values. Later lessons and the capstone reject malformed records explicitly. Skip the header before comparing numeric fields. A strict boundary such as less than 20 differs from less than or equal to 20.

Awk has its own associative arrays; using them does not mean your Bash script uses Bash 4 associative arrays. We keep the surrounding Bash compatible with 3.2 and use awk features shared by the native macOS and GNU implementations.

Try an example.sh
awk -F '\t' 'NR > 1 && $3 < 20 {printf "%s\t%d\n", $1, 100-$3}' capacity.tsv
YOUR TURN

Put it into practice.

Read `capacity.tsv` with header `asset<TAB>service<TAB>free_percent`. For rows with free_percent strictly below 20, write `asset<TAB>used_percent` to `low-space.tsv` without a header, retaining input order. used_percent is 100 minus free_percent. All data rows have valid integer percentages 0–100.

See an explained solution
One way to solve it.sh
awk -F '\t' 'NR > 1 && $3 < 20 {printf "%s\t%d\n", $1, 100-$3}' capacity.tsv > low-space.tsv

Awk compares the numeric field, calculates used space and writes only matching rows. A header-only file naturally produces an empty result.