y
y
yq
Search…
String Operators

RegEx

This uses golangs native regex functions under the hood - See https://github.com/google/re2/wiki/Syntax for the supported syntax.

String blocks, bash and newlines

Bash is notorious for chomping on precious trailing newline characters, making it tricky to set strings with newlines properly. In particular, the $( exp ) will trim trailing newlines.
For instance to get this yaml:
1
a: |
2
cat
Copied!
Using $( exp ) wont work, as it will trim the trailing new line.
1
m=$(echo "cat\n") yq e -n '.a = strenv(m)'
2
a: cat
Copied!
However, using printf works:
1
printf -v m "cat\n" ; m="$m" yq e -n '.a = strenv(m)'
2
a: |
3
cat
Copied!
As well as having multiline expressions:
1
m="cat
2
" yq e -n '.a = strenv(m)'
3
a: |
4
cat
Copied!
Similarly, if you're trying to set the content from a file, and want a trailing new line:
1
IFS= read -rd '' output < <(cat my_file)
2
output=$output ./yq e '.data.values = strenv(output)' first.yml
Copied!

Join strings

Given a sample.yml file of:
1
- cat
2
- meow
3
- 1
4
- null
5
- true
Copied!
then
1
yq eval 'join("; ")' sample.yml
Copied!
will output
1
cat; meow; 1; ; true
Copied!

Match string

Given a sample.yml file of:
1
foo bar foo
Copied!
then
1
yq eval 'match("foo")' sample.yml
Copied!
will output
1
string: foo
2
offset: 0
3
length: 3
4
captures: []
Copied!

Match string, case insensitive

Given a sample.yml file of:
1
foo bar FOO
Copied!
then
1
yq eval '[match("(?i)foo"; "g")]' sample.yml
Copied!
will output
1
- string: foo
2
offset: 0
3
length: 3
4
captures: []
5
- string: FOO
6
offset: 8
7
length: 3
8
captures: []
Copied!

Match with capture groups

Given a sample.yml file of:
1
abc abc
Copied!
then
1
yq eval '[match("(abc)+"; "g")]' sample.yml
Copied!
will output
1
- string: abc
2
offset: 0
3
length: 3
4
captures:
5
- string: abc
6
offset: 0
7
length: 3
8
- string: abc
9
offset: 4
10
length: 3
11
captures:
12
- string: abc
13
offset: 4
14
length: 3
Copied!

Match with named capture groups

Given a sample.yml file of:
1
foo bar foo foo foo
Copied!
then
1
yq eval '[match("foo (?P<bar123>bar)? foo"; "g")]' sample.yml
Copied!
will output
1
- string: foo bar foo
2
offset: 0
3
length: 11
4
captures:
5
- string: bar
6
offset: 4
7
length: 3
8
name: bar123
9
- string: foo foo
10
offset: 12
11
length: 8
12
captures:
13
- string: null
14
offset: -1
15
length: 0
16
name: bar123
Copied!

Capture named groups into a map

Given a sample.yml file of:
1
xyzzy-14
Copied!
then
1
yq eval 'capture("(?P<a>[a-z]+)-(?P<n>[0-9]+)")' sample.yml
Copied!
will output
1
a: xyzzy
2
n: "14"
Copied!

Match without global flag

Given a sample.yml file of:
1
cat cat
Copied!
then
1
yq eval 'match("cat")' sample.yml
Copied!
will output
1
string: cat
2
offset: 0
3
length: 3
4
captures: []
Copied!

Match with global flag

Given a sample.yml file of:
1
cat cat
Copied!
then
1
yq eval '[match("cat"; "g")]' sample.yml
Copied!
will output
1
- string: cat
2
offset: 0
3
length: 3
4
captures: []
5
- string: cat
6
offset: 4
7
length: 3
8
captures: []
Copied!

Test using regex

Like jq'q equivalent, this works like match but only returns true/false instead of full match details
Given a sample.yml file of:
1
- cat
2
- dog
Copied!
then
1
yq eval '.[] | test("at")' sample.yml
Copied!
will output
1
true
2
false
Copied!

Substitute / Replace string

This uses golang regex, described here Note the use of |= to run in context of the current string value.
Given a sample.yml file of:
1
a: dogs are great
Copied!
then
1
yq eval '.a |= sub("dogs", "cats")' sample.yml
Copied!
will output
1
a: cats are great
Copied!

Substitute / Replace string with regex

This uses golang regex, described here Note the use of |= to run in context of the current string value.
Given a sample.yml file of:
1
a: cat
2
b: heat
Copied!
then
1
yq eval '.[] |= sub("(a)", "${1}r")' sample.yml
Copied!
will output
1
a: cart
2
b: heart
Copied!

Split strings

Given a sample.yml file of:
1
cat; meow; 1; ; true
Copied!
then
1
yq eval 'split("; ")' sample.yml
Copied!
will output
1
- cat
2
- meow
3
- "1"
4
- ""
5
- "true"
Copied!

Split strings one match

Given a sample.yml file of:
1
word
Copied!
then
1
yq eval 'split("; ")' sample.yml
Copied!
will output
1
- word
Copied!
Last modified 1mo ago