2022-03-15 23:16:49 +00:00
|
|
|
# Equals / Not Equals
|
2021-11-03 04:00:58 +00:00
|
|
|
|
|
|
|
This is a boolean operator that will return `true` if the LHS is equal to the RHS and `false` otherwise.
|
|
|
|
|
|
|
|
```
|
|
|
|
.a == .b
|
|
|
|
```
|
|
|
|
|
|
|
|
It is most often used with the select operator to find particular nodes:
|
|
|
|
|
|
|
|
```
|
|
|
|
select(.a == .b)
|
|
|
|
```
|
|
|
|
|
2022-03-15 23:16:49 +00:00
|
|
|
The not equals `!=` operator returns `false` if the LHS is equal to the RHS.
|
|
|
|
|
2022-03-17 03:08:08 +00:00
|
|
|
## Related Operators
|
|
|
|
|
2022-03-29 00:08:50 +00:00
|
|
|
- comparison (`>=`, `<` etc) operators [here](https://mikefarah.gitbook.io/yq/operators/compare)
|
|
|
|
- boolean operators (`and`, `or`, `any` etc) [here](https://mikefarah.gitbook.io/yq/operators/boolean-operators)
|
|
|
|
- select operator [here](https://mikefarah.gitbook.io/yq/operators/select)
|
2022-03-17 03:08:08 +00:00
|
|
|
|
|
|
|
|
2021-11-03 04:00:58 +00:00
|
|
|
## Match string
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- cat
|
|
|
|
- goat
|
|
|
|
- dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] | (. == "*at")' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
true
|
|
|
|
true
|
|
|
|
false
|
|
|
|
```
|
|
|
|
|
|
|
|
## Don't match string
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- cat
|
|
|
|
- goat
|
|
|
|
- dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] | (. != "*at")' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
false
|
|
|
|
false
|
|
|
|
true
|
|
|
|
```
|
|
|
|
|
|
|
|
## Match number
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
- 5
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] | (. == 4)' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
false
|
|
|
|
true
|
|
|
|
false
|
|
|
|
```
|
|
|
|
|
2022-05-24 08:18:27 +00:00
|
|
|
## Don't match number
|
2021-11-03 04:00:58 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
- 5
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq '.[] | (. != 4)' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
true
|
|
|
|
false
|
|
|
|
true
|
|
|
|
```
|
|
|
|
|
|
|
|
## Match nulls
|
|
|
|
Running
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq --null-input 'null == ~'
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
true
|
|
|
|
```
|
|
|
|
|
2022-05-25 00:54:56 +00:00
|
|
|
## Non existent key doesn't equal a value
|
2021-11-03 04:00:58 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: frog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq 'select(.b != "thing")' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: frog
|
|
|
|
```
|
|
|
|
|
2021-11-26 09:24:21 +00:00
|
|
|
## Two non existent keys are equal
|
2021-11-03 04:00:58 +00:00
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: frog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq 'select(.b == .c)' sample.yml
|
2021-11-03 04:00:58 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: frog
|
|
|
|
```
|
|
|
|
|