2021-11-03 04:00:58 +00:00
|
|
|
# Style
|
|
|
|
|
|
|
|
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style)
|
|
|
|
|
2022-02-06 03:39:46 +00:00
|
|
|
{% hint style="warning" %}
|
|
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
|
|
|
|
`yq e <exp> <file>`
|
|
|
|
{% endhint %}
|
|
|
|
|
2021-11-03 04:00:58 +00:00
|
|
|
## Update and set style of a particular node (simple)
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
b: thing
|
|
|
|
c: something
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.a.b = "new" | .a.b style="double"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
b: "new"
|
|
|
|
c: something
|
|
|
|
```
|
|
|
|
|
|
|
|
## Update and set style of a particular node using path variables
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
b: thing
|
|
|
|
c: something
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq 'with(.a.b ; . = "new" | . style="double")' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
b: "new"
|
|
|
|
c: something
|
|
|
|
```
|
|
|
|
|
|
|
|
## Set tagged style
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.. style="tagged"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
!!map
|
|
|
|
a: !!str cat
|
|
|
|
b: !!int 5
|
|
|
|
c: !!float 3.2
|
|
|
|
e: !!bool true
|
|
|
|
```
|
|
|
|
|
|
|
|
## Set double quote style
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.. style="double"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: "cat"
|
|
|
|
b: "5"
|
|
|
|
c: "3.2"
|
|
|
|
e: "true"
|
|
|
|
```
|
|
|
|
|
|
|
|
## Set double quote style on map keys too
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '... style="double"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
"a": "cat"
|
|
|
|
"b": "5"
|
|
|
|
"c": "3.2"
|
|
|
|
"e": "true"
|
|
|
|
```
|
|
|
|
|
|
|
|
## Set single quote style
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.. style="single"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: 'cat'
|
|
|
|
b: '5'
|
|
|
|
c: '3.2'
|
|
|
|
e: 'true'
|
|
|
|
```
|
|
|
|
|
|
|
|
## Set literal quote style
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.. style="literal"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: |-
|
|
|
|
cat
|
|
|
|
b: |-
|
|
|
|
5
|
|
|
|
c: |-
|
|
|
|
3.2
|
|
|
|
e: |-
|
|
|
|
true
|
|
|
|
```
|
|
|
|
|
|
|
|
## Set folded quote style
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.. style="folded"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: >-
|
|
|
|
cat
|
|
|
|
b: >-
|
|
|
|
5
|
|
|
|
c: >-
|
|
|
|
3.2
|
|
|
|
e: >-
|
|
|
|
true
|
|
|
|
```
|
|
|
|
|
|
|
|
## Set flow quote style
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.. style="flow"' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
{a: cat, b: 5, c: 3.2, e: true}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Reset style - or pretty print
|
|
|
|
Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
"b": 5
|
|
|
|
'c': 3.2
|
|
|
|
"e": true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '... style=""' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
|
|
|
|
|
|
|
## Set style relatively with assign-update
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: single
|
|
|
|
b: double
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] style |= .' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: 'single'
|
|
|
|
b: "double"
|
|
|
|
```
|
|
|
|
|
|
|
|
## Read style
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
{a: "cat", b: 'thing'}
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.. | style' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
flow
|
|
|
|
double
|
|
|
|
single
|
|
|
|
```
|
|
|
|
|