>_ IzzyLab

Hands-on task

This task is step 18 of the course “sed: the stream editor”

Turn config keys into variable names

hard~10 min

In /etc/app.env the keys are lowercase with hyphens (db-host=pg-1.local). Convert the key names to environment variable format: uppercase letters and underscores instead of hyphens (DB_HOST=pg-1.local). The values to the right of = must not be touched — neither their case nor their hyphens.

What you'll practise:
  • sed
  • \U case conversion
  • capture groups
  • ta/:label loop
  • in-place -i

Topic breakdown

GNU sed extends the replacement with case-conversion escapes: \U/\L switch to upper/lower case until the end of the replacement or until \E, \u/\l apply to the next character only. A separate technique is branching: the label :a together with the ta command ("jump to the label if the last substitution succeeded") forms a loop that repeats the substitution until there are no more matches. It's needed where the g flag doesn't apply — for example, when the pattern is anchored to the start of the line and each match changes what is to its left.

What's checked

  1. all keys are uppercase letters and underscores
  2. the keys kept all their parts
  3. the values to the right of = are unchanged

What it looks like