Skip to content
Latchkey

sed to Edit YAML Values in CI Pipelines

sed can update a YAML value by matching its key and indentation, but it has no understanding of YAML structure.

Pipelines often patch a single YAML field, an image tag or a replica count. sed handles that if you anchor on the key and keep the indentation intact.

What it does

sed treats YAML as plain text. To change a value you match the key line and replace the part after the colon, preserving leading spaces so indentation stays valid. sed cannot resolve anchors, multi-doc files, or nested keys reliably.

Common usage

Terminal
# update an image tag, keeping indentation
sed -i 's|\(  image: myapp:\).*|\11.3.0|' deploy.yml

# change replicas: N
sed -i 's/^\( *replicas:\).*/\1 3/' deploy.yml

# the structure-aware alternative
yq -i '.spec.replicas = 3' deploy.yml

Options

ConcernHow to handle it
IndentationCapture the leading spaces in a group and reuse them
Colon valuesUse a non-slash delimiter when the value has slashes
Key collisionsAnchor with ^ and the exact key to avoid wrong matches
Nested keyssed cannot scope by path; prefer yq for nesting
Quotessed will not add or balance YAML quoting for you

In CI

For one flat key, sed is fine and needs no extra tool on the runner. For anything nested or structural, install yq, which understands YAML and will not corrupt indentation. Remember the GNU vs BSD -i split when the same script runs on both runner types.

Common errors in CI

A replacement that drops the leading spaces produces a YAML parse error downstream such as mapping values are not allowed in this context, because the key is no longer correctly indented. Using / as the delimiter on a value like an image path triggers sed: -e expression #1, char N: unknown option to `s'.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →