yq/pkg/yqlib/doc/operators/length.md

67 lines
728 B
Markdown
Raw Normal View History

2021-11-03 04:00:58 +00:00
# Length
Returns the lengths of the nodes. Length is defined according to the type of the node.
## String length
returns length of string
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
{a: cat}
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.a | length' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
3
```
## null length
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
{a: null}
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.a | length' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
0
```
## Map length
returns number of entries
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
{a: cat, c: dog}
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'length' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2
```
## Array length
returns number of elements
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
[2, 4, 6, 8]
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'length' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
4
```