← All Exercises

Parse INI Sections with awk

intermediatebash~20min

Background

Shell installers often read INI-style settings. A small awk state machine avoids sourcing untrusted files while still handling sections and spacing.

Problem

Read an INI file and print every setting as section.key=value, using default for keys before the first section. An optional section name limits output to that section.

Input

An INI file path and an optional --section NAME. Keys may have spaces around them; comment lines start with # or semicolon.

Output

One section.key=value line per setting in file order with keys and values trimmed, or no output when the requested section is absent.

Sample tests

Sample 1

Input: ini with 'timeout=30', 'debug = false', then [server] with 'host=localhost' and 'port = 8080'
Expected: default.timeout=30
default.debug=false
server.host=localhost
server.port=8080

Sample 2

Input: the same file with --section server
Expected: server.host=localhost
server.port=8080

Your solution

Learning objective

Implement an awk state machine for sections, trim fields safely, and support optional CLI filtering.