yq/operators/equals.md

131 lines
1.4 KiB
Markdown
Raw Normal View History

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