yq/operators/equals.md

134 lines
1.7 KiB
Markdown
Raw Normal View History

2022-03-20 02:05:09 +00:00
# Equals / Not Equals
2021-10-30 03:14:39 +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-20 02:05:09 +00:00
The not equals `!=` operator returns `false` if the LHS is equal to the RHS.
2022-03-28 03:24:57 +00:00
## Related Operators
2022-03-29 00:12:22 +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-28 03:24:57 +00:00
2021-10-30 03:14:39 +00:00
## Match string
Given a sample.yml file of:
```yaml
- cat
- goat
- dog
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.[] | (. == "*at")' sample.yml
2021-10-30 03:14:39 +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-28 01:45:43 +00:00
yq '.[] | (. != "*at")' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
false
false
true
```
## Match number
Given a sample.yml file of:
```yaml
- 3
- 4
- 5
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.[] | (. == 4)' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
false
true
false
```
2022-05-25 01:01:35 +00:00
## Don't match number
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
- 3
- 4
- 5
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.[] | (. != 4)' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
true
false
true
```
## Match nulls
Running
```bash
2022-01-28 01:45:43 +00:00
yq --null-input 'null == ~'
2021-10-30 03:14:39 +00:00
```
will output
```yaml
true
```
2022-05-25 01:01:35 +00:00
## Non existent key doesn't equal a value
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
a: frog
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'select(.b != "thing")' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: frog
```
## Two non existent keys are equal
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
a: frog
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'select(.b == .c)' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: frog
```
2021-11-03 04:00:28 +00:00