yq/usage/output-format.md

103 lines
1.9 KiB
Markdown
Raw Normal View History

2021-10-30 03:14:39 +00:00
---
description: Flags to control yaml and json output format
---
# Output format
2022-12-05 05:49:31 +00:00
These flags are available for all `yq` commands.
2021-10-30 03:14:39 +00:00
## Color
2022-12-05 05:49:31 +00:00
By default, `yq` prints with colours if it detects a terminal. You can manually change this by using either
2021-10-30 03:14:39 +00:00
2022-12-05 05:49:31 +00:00
The `--colors/-C` flag to force print with colors.
2021-10-30 03:14:39 +00:00
2022-12-05 05:49:31 +00:00
The `--no-colors/-M` flag to force print without colours
2021-10-30 03:14:39 +00:00
## Pretty Print
2022-03-08 03:04:38 +00:00
To print out idiomatic `yaml` use the `--prettyPrint/-P` flag. Note that this is shorthand for using the [style](https://mikefarah.gitbook.io/yq/operators/style) operator `... style=""`
2021-10-30 03:14:39 +00:00
## Indent
2022-12-05 05:49:31 +00:00
Use the indent flag `--indent/-I` to control the number of spaces used for indentation. This also works for JSON output. The default value is 2.
2021-10-30 03:14:39 +00:00
Note that lists are indented at the same level as the map key at indent level 2, but are more deeply indented at indent level 4 and greater. This is (currently) a quirk of the underlying [yaml parser](https://github.com/go-yaml/yaml/tree/v3).
Given:
```
apples:
collection:
- name: Green
- name: Blue
favourite: Pink Lady
```
Then:
```
2022-01-28 01:50:13 +00:00
yq -I4 sample.yaml
2021-10-30 03:14:39 +00:00
```
Will print out:
```yaml
apples:
collection:
- name: Green
- name: Blue
favourite: Pink Lady
```
This also works with json
```
2022-01-28 01:50:13 +00:00
yq -j -I4 sample.yaml
2021-10-30 03:14:39 +00:00
```
yields
```javascript
{
"apples": {
"collection": [
{
"name": "Green"
},
{
"name": "Blue"
}
],
"favourite": "Pink Lady"
}
}
```
## Unwrap scalars
By default scalar values are 'unwrapped', that is only their value is printed (except when outputting as JSON). To print out the node as-is, with the original formatting an any comments pass in `--unwrapScalar=false`
Given data.yml:
```yaml
a: "Things" # cool stuff
```
Then:
2022-01-28 01:50:13 +00:00
`yq --unwrapScalar=false '.a' data.yml`
2021-10-30 03:14:39 +00:00
Will yield:
```yaml
"Things" # cool stuff
```
where as without setting the flag to false you would get:
```yaml
Things
```