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.
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:
You can read values safely like this:
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:
If you want the value from that simple line, you can pair grep with sed:
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:
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
grepto 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
yqvariants. Different tools share the same name, so command syntax can vary between implementations.
Summary
- '
grepis acceptable for quick searches in simple, flat YAML.' - '
yqis the right tool when you need a reliable value from a YAML path.' - '
awkcan 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
- How to increase weight of a word for CountVectorizer
- How to initialize word-embeddings for Out of Vocabulary Word?
- How to load the saved tokenizer from pretrained model
- How to make the tensorflow hub embeddings servable using tensorflow serving?
- How to make use of pre-trained word embeddings when training a model in sklearn?
- How to Merge Numerical and Embedding Sequential Models to treat categories in \`RNN\`
- How to Merge Numerical and Embedding Sequential Models to treat categories in `RNN`
- How to parse product titles unstructured into structured data?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.