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

114 lines
1.5 KiB
Markdown
Raw Normal View History

2020-11-22 02:16:54 +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. These are most commonly used with the `select` operator to filter particular nodes.
## 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
```
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
```