yq/pkg/yqlib/doc/Style.md

240 lines
2.8 KiB
Markdown
Raw Normal View History

2020-11-18 09:42:37 +00:00
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style)
2021-04-26 04:17:52 +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
yq eval '.a.b = "new" | .a.b style="double"' sample.yml
```
will output
```yaml
a:
b: "new"
c: something
```
## Update and set style of a particular node using path variables
You can use a variable to re-use a path
Given a sample.yml file of:
```yaml
a:
b: thing
c: something
```
then
```bash
yq eval '.a.b as $x | $x = "new" | $x style="double"' sample.yml
```
will output
```yaml
a:
b: "new"
c: something
```
2020-11-22 02:16:54 +00:00
## Set tagged style
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
2020-11-18 09:42:37 +00:00
b: 5
c: 3.2
e: true
2020-11-17 22:44:16 +00:00
```
then
```bash
2020-11-18 09:42:37 +00:00
yq eval '.. style="tagged"' sample.yml
2020-11-17 22:44:16 +00:00
```
will output
```yaml
2020-11-18 09:42:37 +00:00
!!map
a: !!str cat
b: !!int 5
c: !!float 3.2
e: !!bool true
```
2020-11-22 02:16:54 +00:00
## Set double quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="double"' sample.yml
```
will output
```yaml
a: "cat"
b: "5"
c: "3.2"
e: "true"
```
2020-12-28 00:24:42 +00:00
## 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
yq eval '... style="double"' sample.yml
```
will output
```yaml
"a": "cat"
"b": "5"
"c": "3.2"
"e": "true"
```
2020-11-22 02:16:54 +00:00
## Set single quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="single"' sample.yml
```
will output
```yaml
a: 'cat'
b: '5'
c: '3.2'
e: 'true'
```
2020-11-22 02:16:54 +00:00
## Set literal quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="literal"' sample.yml
```
will output
```yaml
a: |-
cat
b: |-
5
c: |-
3.2
e: |-
true
```
2020-11-22 02:16:54 +00:00
## Set folded quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="folded"' sample.yml
```
will output
```yaml
a: >-
cat
b: >-
5
c: >-
3.2
e: >-
true
```
2020-11-22 02:16:54 +00:00
## Set flow quote style
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="flow"' sample.yml
```
will output
```yaml
{a: cat, b: 5, c: 3.2, e: true}
```
2021-01-06 09:37:53 +00:00
## Reset style - or pretty print
2020-12-29 11:32:06 +00:00
Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.
2020-11-22 02:16:54 +00:00
2020-11-18 09:42:37 +00:00
Given a sample.yml file of:
```yaml
a: cat
2020-12-28 00:24:42 +00:00
"b": 5
'c': 3.2
"e": true
2020-11-18 09:42:37 +00:00
```
then
```bash
2020-12-28 00:24:42 +00:00
yq eval '... style=""' sample.yml
2020-11-18 09:42:37 +00:00
```
will output
```yaml
a: cat
b: 5
c: 3.2
e: true
2020-11-17 22:44:16 +00:00
```
2021-01-06 09:37:53 +00:00
## Set style relatively with assign-update
Given a sample.yml file of:
```yaml
a: single
b: double
```
then
```bash
yq eval '.[] style |= .' sample.yml
```
will output
```yaml
a: 'single'
b: "double"
```
2020-11-22 02:16:54 +00:00
## Read style
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
2020-11-22 02:16:54 +00:00
{a: "cat", b: 'thing'}
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '.. | style' sample.yml
```
will output
```yaml
2020-11-22 02:16:54 +00:00
flow
double
single
2020-11-17 22:44:16 +00:00
```