2021-11-03 04:00:58 +00:00
|
|
|
# Collect into Array
|
|
|
|
|
|
|
|
This creates an array using the expression between the square brackets.
|
|
|
|
|
|
|
|
|
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
|
|
|
## Collect empty
|
|
|
|
Running
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq --null-input '[]'
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
[]
|
|
|
|
```
|
|
|
|
|
|
|
|
## Collect single
|
|
|
|
Running
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq --null-input '["cat"]'
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- cat
|
|
|
|
```
|
|
|
|
|
|
|
|
## Collect many
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '[.a, .b]' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- cat
|
|
|
|
- dog
|
|
|
|
```
|
|
|
|
|