yq
expressions are made up of operators and pipes. A context of nodes is passed through the expression and each operation takes the context as input and returns a new context as output. That output is piped in as input for the next operation in the expression. To begin with, the context is set to the first yaml document of the first yaml file (if processing in sequence using eval).=
operator takes two arguments, a lhs
expression, which in this case is .a
and rhs
expression which is .b
.lhs
expression of .a
to return the node=
operator then pipes the 'root' context through the rhs
expression of .b
to return the node.b
) to the the LHS (.a
), and it returns the now updated context:yq
expressions have an order of precedence. The pipe |
operator has a low order of precedence, so operators with higher precedence will get evaluated first..a = "cat" | .b = "dog"
is effectively: (.a = "cat") | (.b = "dog")
.sally
entry to have fruit: 'mango'. The incorrect way to do that is: .[] | select(.name == "sally") | .fruit = "mango"
.|
has a low operator precedence, this will be evaluated (incorrectly) as : (.[]) | (select(.name == "sally")) | (.fruit = "mango")
. What you'll see is only the updated segment returned:(.[] | select(.name == "sally") | .fruit) = "mango"
=
) operator, and the yaml is correctly updated and returned:|=
)=
operator which we call the relative form. It's very similar to =
but with one key difference when evaluating the RHS expression.=
operator, |=
takes two operands, the LHS and RHS..a
to get the node value:. + 1
(whereas in the =
plain form it piped the original document context into the RHS) to yield: