2021-01-14 04:05:50 +00:00
|
|
|
# String Operators
|
2021-01-14 03:46:50 +00:00
|
|
|
|
|
|
|
## Join strings
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- cat
|
|
|
|
- meow
|
|
|
|
- 1
|
|
|
|
- null
|
|
|
|
- true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval 'join("; ")' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
cat; meow; 1; ; true
|
|
|
|
```
|
|
|
|
|
2021-04-15 00:09:41 +00:00
|
|
|
## Substitute / Replace string
|
|
|
|
This uses golang regex, described [here](https://github.com/google/re2/wiki/Syntax)
|
2021-04-16 06:07:40 +00:00
|
|
|
Note the use of `|=` to run in context of the current string value.
|
2021-04-15 00:09:41 +00:00
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: dogs are great
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval '.a |= sub("dogs", "cats")' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: cats are great
|
|
|
|
```
|
|
|
|
|
|
|
|
## Substitute / Replace string with regex
|
|
|
|
This uses golang regex, described [here](https://github.com/google/re2/wiki/Syntax)
|
2021-04-16 06:07:40 +00:00
|
|
|
Note the use of `|=` to run in context of the current string value.
|
2021-04-15 00:09:41 +00:00
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: heat
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2021-04-16 06:07:40 +00:00
|
|
|
yq eval '.[] |= sub("(a)", "${1}r")' sample.yml
|
2021-04-15 00:09:41 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: cart
|
|
|
|
b: heart
|
|
|
|
```
|
|
|
|
|
2021-01-14 04:05:50 +00:00
|
|
|
## Split strings
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
cat; meow; 1; ; true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval 'split("; ")' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- cat
|
|
|
|
- meow
|
|
|
|
- "1"
|
|
|
|
- ""
|
|
|
|
- "true"
|
|
|
|
```
|
|
|
|
|
|
|
|
## Split strings one match
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
word
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
|
|
|
yq eval 'split("; ")' sample.yml
|
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- word
|
|
|
|
```
|
|
|
|
|