[TIP] Processing YAML files with yq

After the success of the jq - tool for parsing and manipulating JSON-Data someone wrote a tool called yq , which aims to be the same for YAML, what jq is for JSON. Seems to work fine. I'll definitely give it a chance in future.

Example YAML-File:

--- !ruby/object:Puppet::Node::Facts
  values:
    processors:
      models:
        - "Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz"
      count: 1
      physicalcount: 1
    kernel: Linux
    architecture: x86_64
    hardwaremodel: x86_64
    operatingsystem: CentOS
    os:
      name: CentOS
      family: RedHat
      release:
        major: "6"
        minor: "10"
        full: "6.10"
    processor0: "Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz"

yq - Usage:

# Command
yq -r .values.os.family data.yaml

# Output
RedHat

It's already available via repository in newer versions of major linux distributions

See also:
Yq

6 Likes

I just read that yq is not a standalone script, but does only a data transition from YAML to JSON and then feeds the data into jq with the user-given jq-expression and thus relies on jq to be installed. It comes with another tool named xq which works the same way with XML.