Updated var to work like jq #934

This commit is contained in:
Mike Farah
2021-09-12 16:55:55 +10:00
parent 1cfbbde796
commit b2ee131a4c
9 changed files with 102 additions and 13 deletions
+2 -2
View File
@@ -18,7 +18,7 @@ a:
```
## Update and set style of a particular node using path variables
You can use a variable to re-use a path
You can use a variable reference to re-use a path
Given a sample.yml file of:
```yaml
@@ -28,7 +28,7 @@ a:
```
then
```bash
yq eval '.a.b as $x | $x = "new" | $x style="double"' sample.yml
yq eval '.a.b ref $x | $x = "new" | $x style="double"' sample.yml
```
will output
```yaml
+37 -1
View File
@@ -1,4 +1,6 @@
For more complex scenarios, variables can be used to hold values of expression to be used in other expressions.
Like the `jq` equivalents, variables are sometimes required for the more complex expressions (or swapping values between fields).
Note that there is also an additional `ref` operator that holds a reference (instead of a copy) of the path, allowing you to make multiple changes to the same path.
## Single value variable
Given a sample.yml file of:
@@ -56,3 +58,37 @@ title: A well-written article
author: Person McPherson
```
## Using variables to swap values
Given a sample.yml file of:
```yaml
a: a_value
b: b_value
```
then
```bash
yq eval '.a as $x | .b as $y | .b = $x | .a = $y' sample.yml
```
will output
```yaml
a: b_value
b: a_value
```
## Use ref to reference a path repeatedly
Given a sample.yml file of:
```yaml
a:
b: thing
c: something
```
then
```bash
yq eval '.a.b ref $x | $x = "new" | $x style="double"' sample.yml
```
will output
```yaml
a:
b: "new"
c: something
```
+3 -1
View File
@@ -1 +1,3 @@
For more complex scenarios, variables can be used to hold values of expression to be used in other expressions.
Like the `jq` equivalents, variables are sometimes required for the more complex expressions (or swapping values between fields).
Note that there is also an additional `ref` operator that holds a reference (instead of a copy) of the path, allowing you to make multiple changes to the same path.