-
-
Notifications
You must be signed in to change notification settings - Fork 23
yaml get
The yaml-get
command-line tool queries YAML/JSON/compatible data, returning the results to STDOUT. The input data can be read from a single file or STDIN (when using the -
pseudo-file). Results are printed one per line to simplify downstream parsing. Complex results (Hashes and Arrays) are rendered as JSON so they can also be printed one result per line. EYAML files can also be queried, returning decrypted results when the appropriate encryption keys are supplied.
Users instruct yaml-get
where to find its results via the --query
(-p
) argument, which accepts a YAML Path. Should there be any issues automatically inferring the segment notation (dot [.] versus forward-slash [/]) used for this argument, the --pathsep
(-t
) argument can be used to force the notation.
Data access using YAML Paths via yaml-get
is already well covered in the various discussions about Segments-of-a-YAML-Path and will not be repeated here. Rather, this page will present some tasks which can be solved by using yaml-get
and in some cases, in concert with other YAML Path tools.
Scalar data is any single, simple piece of data like a number, string (text), or boolean (true/false). In the simplest case, you want just one such piece of data from a YAML/JSON/EYAML/compatible file into your shell script. Take for example this data file:
File: get1.yaml
this:
is:
a:
hash: data
structure: with
several: levels
Suppose you're after whatever data is at this.several
(also /this/several
):
#!/bin/bash
# Read the Scalar value into a variable your script can use
thisSeveral=$(yaml-get --query=this.several get1.yaml)
# Do something with the value
echo "Got: ${thisSeveral}"
This prints: Got: levels
.
In some cases, the Scalar you want is exactly one element of an Array.
File: get2.yaml
another:
hash:
- with
- its
- own
child:
- nodes
- and
- data
Should you need the second entry of the Array at /another/child
in your script, you would:
#!/bin/bash
# Read the Scalar value into a variable your script can use
thisSeveral=$(yaml-get --query=/another/child[1] get2.yaml)
# Do something with the value
echo "Got: ${thisSeveral}"
This prints Got: and
.