mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Tweaking docs
This commit is contained in:
parent
b1292270bb
commit
8343ff6a59
@ -66,20 +66,28 @@ Lets say you had:
|
|||||||
Lets say you wanted to update the `sally` entry to have fruit: 'mango'. The _incorrect_ way to do that is:
|
Lets say you wanted to update the `sally` entry to have fruit: 'mango'. The _incorrect_ way to do that is:
|
||||||
`.[] | select(.name == "sally") | .fruit = "mango"`.
|
`.[] | select(.name == "sally") | .fruit = "mango"`.
|
||||||
|
|
||||||
Becasue `|` has a low operator precedence, this will be evaluated (_incorrectly_) as : `(.[]) | (select(.name == "sally")) | (.fruit = "mango")`. What you'll see is only:
|
Becasue `|` 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:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
name: sally
|
name: sally
|
||||||
fruit: mango
|
fruit: mango
|
||||||
```
|
```
|
||||||
|
|
||||||
Returned :(
|
To properly update this yaml, you will need to use brackets (think BODMAS from maths) and wrap the entire LHS:
|
||||||
|
|
||||||
|
|
||||||
In this case, you will need to use brackets (think BODMAS from maths) and wrap the entire LHS, so the _correct_ expression is:
|
|
||||||
`(.[] | select(.name == "sally") | .fruit) = "mango"`
|
`(.[] | select(.name == "sally") | .fruit) = "mango"`
|
||||||
|
|
||||||
|
|
||||||
|
Now that entire LHS expression is passed to the 'assign' (`=`) operator, and the yaml is correctly updated and returned:
|
||||||
|
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- name: bob
|
||||||
|
fruit: apple
|
||||||
|
- name: sally
|
||||||
|
fruit: mango
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
## Relative update (e.g. `|=`)
|
## Relative update (e.g. `|=`)
|
||||||
There is another form of the `=` operator which we call the relative form. It's very similar to `=` but with one key difference when evaluating the RHS expression.
|
There is another form of the `=` operator which we call the relative form. It's very similar to `=` but with one key difference when evaluating the RHS expression.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user