Posted on
Software

yq: YAML processing

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Exploring yq: A Powerful YAML Processor for Bash

In the realm of configuration management and development, YAML has become increasingly popular due to its simplicity and readability. However, dealing with YAML files can sometimes be cumbersome, especially from the command line. This is where yq comes into play, a lightweight and portable command-line YAML processor which is inspired by jq - the JSON processor.

What is yq?

yq is a command-line tool that allows you to read, write, and manipulate YAML files easily. It comes in handy for extracting data, manipulating structures, converting formats, and more—all directly from the terminal or in scripts.

Why Use yq?

  1. Simplicity: yq uses a succinct syntax which is easy to write and understand.
  2. Flexibility: It can convert YAML into JSON, making it interoperable with tools that only understand JSON.
  3. Automation: It's perfect for scripting and automation tasks.

Installation Instructions

Getting yq up and running on your Linux system is a straightforward process. Here are the instructions based on different package managers:

Using apt (For Debian-based Distributions):

yq is available in the official repositories for recent versions of Debian-based distributions such as Ubuntu. To install it, simply run:

sudo apt update
sudo apt install yq -y

Using dnf (For Fedora):

For Fedora, yq can also be installed directly from the repository:

sudo dnf install yq

Using zypper (For openSUSE):

Similarly, if you are using openSUSE, you can install yq from the repository by running:

sudo zypper install yq

Docker Alternative

If you prefer using Docker, you can run yq without installing it on your local system. Just use the following Docker command:

docker run --rm -v "${PWD}":/workdir mikefarah/yq yq [commands here]

This command will download the yq Docker image and execute yq with whatever commands you specify, operating on the files in your current directory.

Basic Usage of yq

Now that you have yq installed, here are a few basic commands to get you started:

  • Viewing a YAML file:

    yq e '.root' file.yaml
    
  • Updating a value:

    yq e '.some.path = "newvalue"' -i file.yaml
    
  • Merging two YAML files:

    yq ea '. as $item ireduce ({}; . * $item )' file1.yaml file2.yaml
    
  • Converting YAML to JSON:

    yq ea -j '. as $item ireduce ({}; . * $item )' file.yaml
    

Practical Examples

To give you a better sense of yq's power, here are a couple of practical examples:

  1. Extracting Values: Extract usernames from a list of systems users stored in a YAML file:

    yq e '.users[].name' users.yaml
    
  2. Bulk Updates: Increment version number in multiple documents:

    yq e '.version += 1' -i *.yaml
    

Conclusion

yq is a robust tool that bridges the gap between the human-friendly YAML data format and the automation needs of command-line operations. With its installation being straightforward across various Linux distributions and flexible usage scenarios, yq is an essential utility for anyone handling YAML data regularly. Whether you're a system admin, a developer, or a DevOps professional, mastering yq will surely make your scripts more efficient and your life slightly easier. Happy YAML processing!