yq/usage/csv-tsv.md

48 lines
826 B
Markdown
Raw Normal View History

2022-02-06 03:31:21 +00:00
# Working with CSV, TSV
2021-12-04 03:08:16 +00:00
2022-02-06 03:29:26 +00:00
{% hint style="warning" %}
2022-02-06 03:31:21 +00:00
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
2022-02-06 03:29:26 +00:00
`yq e <exp> <file>`
{% endhint %}
2021-12-04 03:08:16 +00:00
## Yaml to CSV/TSV
You can convert compatible yaml structures to CSV or TSV by using:
2022-02-06 03:31:21 +00:00
2022-07-07 00:27:40 +00:00
* `--output-format=csv` or `-o=c` for csv (comma separated values)
* `--output-format=tsv` or `-o=t` for tsv (tab separated values)
2021-12-04 03:08:16 +00:00
Compatible structures is either an array of scalars (strings/numbers/booleans), which is a single row; or an array of arrays of scalars (multiple rows).
```yaml
- [i, like, csv]
- [because, excel, is, cool]
```
then
```bash
2022-01-28 01:50:13 +00:00
yq '.' -o=csv sample.yaml
2021-12-04 03:08:16 +00:00
```
will output:
```csv
i,like,csv
because,excel,is,cool
```
Similarly, for tsv:
```bash
2022-01-28 01:50:13 +00:00
yq '.' -o=tsv sample.yaml
2021-12-04 03:08:16 +00:00
```
will output:
2022-02-06 03:31:21 +00:00
```
2021-12-04 03:08:16 +00:00
i like csv
because excel is cool
```