Chapter 08Lesson 04~90 minutes

awk for Field-Oriented Processing

`awk` is a compact record-processing language. When a shell pipeline becomes dominated by field extraction, counting, grouping, or numeric calculations, awk is often the clearer abstraction.

BeginnerText processingHands-on lab

Learning objectives

By the end of this lesson

  • Explain awk records and fields.
  • Set field and output separators.
  • Filter and transform records.
  • Aggregate values with associative arrays.
  • Pass shell values safely with -v.

1. awk processes records and fields

awk record model
flowchart LR
  R["record"] --> F["fields"]
  F --> P{"pattern?"}
  P -->|"yes"| A["action"]
  P -->|"no"| N["next record"]
  A --> N

By default, one input line is a record, $0 is the whole record, and $1, $2, ... are fields.

2. Print selected fields

printf '%s\n' 'api prod 3' 'worker staging 2' |
awk '{ print $1, $3 }'

3. Control FS and OFS explicitly

printf '%s\n' 'api:prod:3' 'worker:staging:2' |
awk -F: 'BEGIN { OFS="," } { print $1, $2, $3 }'

4. Patterns combine filtering and processing

awk -F: '$2 == "prod" { print $1 }' services.txt

A single awk program can often replace several tiny extraction/filter stages while keeping the record model visible.

5. Numeric aggregation is natural in awk

awk -F: '
  { total += $3 }
  END { print "replicas=" total }
' services.txt

6. Associative arrays support grouping

awk -F: '
  { count[$2]++ }
  END {
    for (env in count)
      print env, count[env]
  }
' services.txt
Ordering

Do not depend on associative-array iteration order. Sort output when order is part of the contract.

7. printf controls report formatting

awk -F: '
  { printf "%-12s env=%-8s replicas=%d\n", $1, $2, $3 }
' services.txt

8. Pass shell values with -v

environment='prod'

awk -F: -v env="$environment" '
  $2 == env { print $1 }
' services.txt
Separate data from program source

Use -v rather than splicing arbitrary shell text into the awk program.

9. awk can produce a validation status

awk -F: '
  $3 !~ /^[0-9]+$/ { bad=1 }
  END { exit bad ? 1 : 0 }
' services.txt

printf 'status=%d\n' "$?"

10. Hands-on lab: aggregate service inventory

mkdir -p "$HOME/devops-academy/bash/chapter08/lesson04"
cd "$HOME/devops-academy/bash/chapter08/lesson04"

cat > services.txt <<'EOF'
api:prod:3
worker:prod:2
cache:staging:1
metrics:prod:1
scheduler:staging:2
EOF

awk -F: '
BEGIN { OFS="\t" }
{
  services[$2]++
  replicas[$2] += $3
}
END {
  for (env in services)
    print env, services[env], replicas[env]
}
' services.txt |
LC_ALL=C sort

Verification checklist

11. Knowledge check

Question 1. What does $0 mean in awk?

Question 2. How do you set a colon field separator?

Question 3. How should shell data be passed into awk?

Question 4. When is awk often better than a Bash loop?

12. Summary

awk is a small record-processing language. Use it when text manipulation becomes field logic: selection, arithmetic, grouping, counting, and formatted reporting.

13. Further reading

  • GNU Awk User’s Guide.
  • POSIX awk utility specification.
  • GNU Awk documentation on variable assignment.
  • ShellCheck documentation on quoting around awk.
Next lesson

Composing Text Pipelines without Fragile Parsing

Continue Chapter 8 by expanding the text-processing toolkit and its reliability rules.

Keep the academy open

Support free, practical DevOps education.

Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.

Ethereum / ERC-20
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0 Send only Ethereum/ERC-20 compatible assets to this address.