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.
2021-05-14 05:01:44 +00:00
`any_c(condition)` and `all_c(condition)` are like `any` and `all` but they take a condition expression that is used against each element to determine if it's `true` . Note: in `jq` you can simply pass a condition to `any` or `all` and it simply works - `yq` isn't that clever..yet
2021-05-14 04:29:55 +00:00
These are most commonly used with the `select` operator to filter particular nodes.
2021-05-14 05:01:44 +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
```
2021-05-14 05:01:44 +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 05:01:44 +00:00
## `any` returns true if any boolean in a given array is true
2021-05-14 04:29:55 +00:00
Given a sample.yml file of:
```yaml
- false
- true
```
then
```bash
yq eval 'any' sample.yml
```
will output
```yaml
true
```
2021-05-14 05:01:44 +00:00
## `any` returns false for an empty array
2021-05-14 04:29:55 +00:00
Given a sample.yml file of:
```yaml
2021-05-14 05:01:44 +00:00
[]
2021-05-14 04:29:55 +00:00
```
then
```bash
yq eval 'any' sample.yml
```
will output
```yaml
2021-05-14 05:01:44 +00:00
false
2021-05-14 04:29:55 +00:00
```
2021-05-14 05:01:44 +00:00
## `any_c` returns true if any element in the array is true for the given condition.
2021-05-14 04:29:55 +00:00
Given a sample.yml file of:
```yaml
2021-05-14 05:01:44 +00:00
a:
- rad
- awesome
b:
- meh
- whatever
2021-05-14 04:29:55 +00:00
```
then
```bash
2021-05-14 05:01:44 +00:00
yq eval '.[] |= any_c(. == "awesome")' sample.yml
2021-05-14 04:29:55 +00:00
```
will output
```yaml
2021-05-14 05:01:44 +00:00
a: true
b: false
2021-05-14 04:29:55 +00:00
```
2021-05-14 05:01:44 +00:00
## `all` returns true if all booleans in a given array are true
2021-05-14 04:29:55 +00:00
Given a sample.yml file of:
```yaml
- true
- true
```
then
```bash
yq eval 'all' sample.yml
```
will output
```yaml
true
```
2021-05-14 05:01:44 +00:00
## `all` returns true for an empty array
2021-05-14 04:29:55 +00:00
Given a sample.yml file of:
```yaml
[]
```
then
```bash
yq eval 'all' sample.yml
```
will output
```yaml
true
```
2021-05-14 05:01:44 +00:00
## `all_c` returns true if all elements in the array are true for the given condition.
Given a sample.yml file of:
```yaml
a:
- rad
- awesome
b:
- meh
- 12
```
then
```bash
yq eval '.[] |= all_c(tag == "!!str")' sample.yml
```
will output
```yaml
a: true
b: false
```
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
```