grep
yaml
command line
text processing
tutorial

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:

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

Read values 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 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:

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

Pair it with sed to get only the value:

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 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:

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 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 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.

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
Basis Lab
No 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 course
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