yq/pkg/yqlib/doc/operators/equals.md

140 lines
1.8 KiB
Markdown
Raw Normal View History

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
2022-02-06 03:39:46 +00:00
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
`yq e <exp> <file>`
{% endhint %}
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
```
## Dont 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
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
```
## Non exisitant key doesn't equal a value
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
```