How to grep or parse a YAML value in Bash (grep, awk and yq)
Codemia Knowledge Hub ·
grep works for quick searches in simple, flat YAML, but to extract a value reliably, use a YAML-aware tool such as yq. YAML is indentation-sensitive and supports nested mappings, lists, quoted strings, comments, anchors and multiline values, so regex-based extraction breaks as soon as the file gets more complex.
Extracting values with yq
With yq you address the YAML structure directly instead of guessing it from whitespace. Given this file:
Read values like this:
This stays correct for keys nested several levels deep, handles quoted strings and numbers without custom regular expressions, and works naturally with arrays and nested objects. Be aware that different tools share the name yq, so command syntax can vary between implementations.
Using grep on flat YAML
For a flat file where you know there is one top-level host key, grep shows the matching line:
Pair it with sed to get only the value:
That is fine for ad hoc shell work, but it is not a YAML parser. Once the file has nesting or host keys in several sections, the result is ambiguous.
Reading a nested key with awk
For one-off debugging, awk can track indentation well enough to inspect a section. To get server.host from the example file:
This works only because the structure is predictable. It can break on comments, tabs, duplicate keys or block scalars, so keep it for manual inspection, not production scripts.
Picking the right tool
Ask which of two things you need:
- 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
For a quick look at a config file in the terminal, grep is often enough. For a script other people depend on, use yq, because it understands the format instead of approximating it.
Where text-based extraction breaks
- 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.
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 parse product titles unstructured into structured data?
- How to proceed with NLP task for recognizing intent and slots
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.