Read
Returns matching nodes/values of a path expression for a given yaml document
Last updated
Was this helpful?
Returns matching nodes/values of a path expression for a given yaml document
Last updated
Was this helpful?
Returns the matching nodes of the path expression for the given yaml file (or STDIN).
See docs for for more details.
Given a sample.yaml file of:
then
will output the value of '2'.
Given a sample.yaml file of:
will output the value of '2'.
Using the --defaultValue/-D
flag you can specify a default value to be printed when no matching nodes are found for an expression
will yield (assuming path.not.there
does not match any nodes):
By default, yq will only print the value of the path expression for the yaml document. By specifying --printMode
or -p
you can print the matching paths.
will print
will print
By default, results are printed out line by line as independent matches. This is handy for both readability as well as piping into tools like xargs
. However, if you would like to collect the matching results into an array then use the --collect/-c
flag. This is particularly useful with the length
flag described below.
Given:
will print
Use the --length/-l
flag to print the length of results. For arrays this will be the number of items, objects the number of entries and scalars the length of the value.
Given
will print
By default, filtered results are printed independently so you will get the length of each result printed on a separate line:
However, you'll often want to know the count of the filtered results - use the --collect
flag to collect the results in the array. The length will then return the size of the array.
The read command will print out the anchors of a document and can also traverse them.
Lets take a look at a simple example file:
will print out
Similarly,
prints out
To traverse an anchor, we need to either explicitly reference merged in values:
to get
or we can use deep splat to get all the values
prints
The same methods work for the <<: [*blah, *thing]
anchors.
By default anchors are not exploded (or expanded/de-referenced) for viewing, and the yaml is shown as-is. Use the --explodeAnchors/-X
flag to show the anchor values.
Given sample.yml:
Then
yields
Note that yq processes the merge anchor list in reverse order, to ensure that the last items in the list override the preceding.
Given a sample.yaml file of:
then
will output the value of '2'.
Reading all documents will return the result as an array. This can be converted to json using the '-j' flag if desired.
Given a sample.yaml file of:
then
will output: