yq/pkg/yqlib/doc/Boolean Operators.md

193 lines
2.5 KiB
Markdown
Raw Normal View History

2021-05-14 04:29:55 +00:00
The `or` and `and` operators take two parameters and return a boolean result.
`not` flips a boolean from true to false, or vice versa.
`any` will return `true` if there are any `true` values in a array sequence, and `all` will return true if _all_ elements in an array are true.
These are most commonly used with the `select` operator to filter particular nodes.
2020-11-22 02:16:54 +00:00
## OR example
2020-11-20 04:31:49 +00:00
Running
2020-11-20 03:35:34 +00:00
```bash
2020-11-20 04:31:49 +00:00
yq eval --null-input 'true or false'
2020-11-20 03:35:34 +00:00
```
will output
```yaml
2020-11-20 04:31:49 +00:00
true
2020-11-20 03:35:34 +00:00
```
2020-11-22 02:16:54 +00:00
## AND example
2020-11-20 04:31:49 +00:00
Running
2020-11-20 03:35:34 +00:00
```bash
2020-11-20 04:31:49 +00:00
yq eval --null-input 'true and false'
2020-11-20 03:35:34 +00:00
```
will output
```yaml
2020-11-20 04:31:49 +00:00
false
2020-11-20 03:35:34 +00:00
```
2020-11-22 02:16:54 +00:00
## Matching nodes with select, equals and or
2020-11-20 03:35:34 +00:00
Given a sample.yml file of:
```yaml
2020-11-20 04:31:49 +00:00
- a: bird
b: dog
- a: frog
b: bird
- a: cat
b: fly
2020-11-20 03:35:34 +00:00
```
then
```bash
2020-11-20 04:33:21 +00:00
yq eval '[.[] | select(.a == "cat" or .b == "dog")]' sample.yml
2020-11-20 03:35:34 +00:00
```
will output
```yaml
2020-11-20 04:33:21 +00:00
- a: bird
b: dog
- a: cat
b: fly
2020-11-20 03:35:34 +00:00
```
2021-05-14 04:29:55 +00:00
## ANY returns true if any boolean in a given array is true
Given a sample.yml file of:
```yaml
- false
- true
```
then
```bash
yq eval 'any' sample.yml
```
will output
```yaml
true
```
## ANY returns true if any boolean in a given array is true
Given a sample.yml file of:
```yaml
- false
- true
```
then
```bash
yq eval 'any' sample.yml
```
will output
```yaml
true
```
## ANY returns false for an empty array
Given a sample.yml file of:
```yaml
[]
```
then
```bash
yq eval 'any' sample.yml
```
will output
```yaml
false
```
## ALL returns true if all booleans in a given array are true
Given a sample.yml file of:
```yaml
- true
- true
```
then
```bash
yq eval 'all' sample.yml
```
will output
```yaml
true
```
## ANY returns true for an empty array
Given a sample.yml file of:
```yaml
[]
```
then
```bash
yq eval 'all' sample.yml
```
will output
```yaml
true
```
2020-11-22 02:16:54 +00:00
## Not true is false
Running
```bash
yq eval --null-input 'true | not'
```
will output
```yaml
false
```
## Not false is true
Running
```bash
yq eval --null-input 'false | not'
```
will output
```yaml
true
```
## String values considered to be true
Running
```bash
yq eval --null-input '"cat" | not'
```
will output
```yaml
false
```
## Empty string value considered to be true
Running
```bash
yq eval --null-input '"" | not'
```
will output
```yaml
false
```
## Numbers are considered to be true
Running
```bash
yq eval --null-input '1 | not'
```
will output
```yaml
false
```
## Zero is considered to be true
Running
```bash
yq eval --null-input '0 | not'
```
will output
```yaml
false
```
## Null is considered to be false
Running
```bash
yq eval --null-input '~ | not'
```
will output
```yaml
true
```