grep
yaml
command line
text processing
tutorial

How to grep a yaml value

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

You can search YAML with grep, but that only works reliably for very simple files. YAML is indentation-sensitive and supports nested mappings, lists, quoted strings, comments, anchors, and multiline values, so exact value extraction is usually better handled by a YAML-aware tool such as yq.

Use yq When You Need the Actual Value

If the goal is to extract a field rather than merely see matching text, use a parser. With yq, you address the YAML structure directly instead of guessing it from whitespace.

Given this file:

yaml
1server:
2  host: api.example.com
3  port: 8080
4services:
5  - name: api
6    port: 9000
7  - name: worker
8    port: 9001

You can read values safely like this:

bash
yq eval -r '.server.host' config.yaml
yq eval -r '.server.port' config.yaml
yq eval -r '.services[] | select(.name == "api") | .port' config.yaml

This approach stays correct even when keys are nested several levels deep. It also handles quoted strings and numbers without requiring custom regular expressions.

What grep Can Do Well

Plain grep is still useful for quick inspection when the YAML is flat and you only want to see matching lines. For example, if the file contains a top-level host key and you know there are no duplicates:

bash
grep -E '^host:[[:space:]]*' config.yaml

If you want the value from that simple line, you can pair grep with sed:

bash
grep -E '^host:[[:space:]]*' config.yaml | sed -E 's/^host:[[:space:]]*//'

That is fine for ad hoc shell work, but it is not a real YAML parser. As soon as the file contains nesting or multiple host keys in different sections, the result becomes ambiguous.

Handling a Nested Section With Text Tools

For quick one-off debugging, awk can track indentation well enough to inspect a section. Suppose you want server.host from the example file above:

bash
1awk '
2  /^server:/ { in_server=1; next }
3  /^[^[:space:]]/ { in_server=0 }
4  in_server && /^[[:space:]]+host:/ {
5    sub(/^[[:space:]]+host:[[:space:]]*/, "")
6    print
7  }
8' config.yaml

This works because the file structure is predictable, but it remains a heuristic. It can break on comments, tabs, duplicate keys, or block scalars. That is why awk is a fallback for human-driven inspection, not a strong choice for production scripting.

Choose the Tool Based on the Goal

There are really two separate questions:

  • Do you want to find lines that mention a key or value
  • Do you want to parse YAML and return the exact data at a path

If you only want to inspect a config file from the terminal, grep is quick and often good enough. If you are writing a script that other people depend on, yq is the safer choice because it understands the file format instead of approximating it.

Another advantage of yq is that it works naturally with arrays and nested objects. Reproducing that behavior with regular expressions becomes fragile very quickly.

Common Pitfalls

  • Using grep to parse nested YAML and assuming indentation will never change.
  • Forgetting that the same key can appear in multiple sections, which makes a line-based match ambiguous.
  • Breaking on quoted values or values that contain a colon character.
  • Ignoring multiline scalars, anchors, aliases, or multiple YAML documents in one file.
  • Mixing yq variants. Different tools share the same name, so command syntax can vary between implementations.

Summary

  • 'grep is acceptable for quick searches in simple, flat YAML.'
  • 'yq is the right tool when you need a reliable value from a YAML path.'
  • 'awk can help with quick nested inspection, but it is still only a text-based workaround.'
  • YAML structure makes regex-only parsing fragile as files become more complex.
  • For scripts and automation, use a YAML-aware parser instead of guessing from indentation.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.