2023-03-18 03:51:31 +00:00
|
|
|
# Divide
|
|
|
|
|
|
|
|
Divide behaves differently according to the type of the LHS:
|
|
|
|
* strings: split by the divider
|
|
|
|
* number: arithmetic division
|
2023-03-15 09:14:23 +00:00
|
|
|
|
|
|
|
## String split
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-02 05:07:04 +00:00
|
|
|
{a: cat_meow, b: _}
|
2023-03-15 09:14:23 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq '.c = .a / .b' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2023-05-02 05:07:04 +00:00
|
|
|
{a: cat_meow, b: _, c: [cat, meow]}
|
2023-03-15 09:14:23 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Number division
|
|
|
|
The result during division is calculated as a float
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-02 05:07:04 +00:00
|
|
|
{a: 12, b: 2.5}
|
2023-03-15 09:14:23 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq '.a = .a / .b' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2023-05-02 05:07:04 +00:00
|
|
|
{a: 4.8, b: 2.5}
|
2023-03-15 09:14:23 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Number division by zero
|
|
|
|
Dividing by zero results in +Inf or -Inf
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-02 05:07:04 +00:00
|
|
|
{a: 1, b: -1}
|
2023-03-15 09:14:23 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq '.a = .a / 0 | .b = .b / 0' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2023-05-02 05:07:04 +00:00
|
|
|
{a: !!float +Inf, b: !!float -Inf}
|
2023-03-15 09:14:23 +00:00
|
|
|
```
|
|
|
|
|