String Operators
RegEx
This uses Golang's native regex functions under the hood - See their docs for the supported syntax.
Case insensitive tip: prefix the regex with (?i) - e.g. test("(?i)cats").
match(regEx)
This operator returns the substring match details of the given regEx.
capture(regEx)
Capture returns named RegEx capture groups in a map. Can be more convenient than match depending on what you are doing.
test(regEx)
Returns true if the string matches the RegEx, false otherwise.
sub(regEx, replacement)
Substitutes matched substrings. The first parameter is the regEx to match substrings within the original string. The second parameter specifies what to replace those matches with. This can refer to capture groups from the first RegEx.
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:
a: |
catUsing $( exp ) wont work, as it will trim the trailing newline.
m=$(echo "cat\n") yq -n '.a = strenv(m)'
a: catHowever, using printf works:
printf -v m "cat\n" ; m="$m" yq -n '.a = strenv(m)'
a: |
catAs well as having multiline expressions:
m="cat
" yq -n '.a = strenv(m)'
a: |
catSimilarly, if you're trying to set the content from a file, and want a trailing newline:
IFS= read -rd '' output < <(cat my_file)
output=$output ./yq '.data.values = strenv(output)' first.ymlInterpolation
Given a sample.yml file of:
value: things
another: stuffthen
yq '.message = "I like \(.value) and \(.another)"' sample.ymlwill output
value: things
another: stuff
message: I like things and stuffInterpolation - not a string
Given a sample.yml file of:
value:
an: applethen
yq '.message = "I like \(.value)"' sample.ymlwill output
value:
an: apple
message: 'I like an: apple'To up (upper) case
Works with unicode characters
Given a sample.yml file of:
águathen
yq 'upcase' sample.ymlwill output
ÁGUATo down (lower) case
Works with unicode characters
Given a sample.yml file of:
ÁgUAthen
yq 'downcase' sample.ymlwill output
águaJoin strings
Given a sample.yml file of:
- cat
- meow
- 1
- null
- truethen
yq 'join("; ")' sample.ymlwill output
cat; meow; 1; ; trueTrim strings
Given a sample.yml file of:
- ' cat'
- 'dog '
- ' cow cow '
- horsethen
yq '.[] | trim' sample.ymlwill output
cat
dog
cow cow
horseMatch string
Given a sample.yml file of:
foo bar foothen
yq 'match("foo")' sample.ymlwill output
string: foo
offset: 0
length: 3
captures: []Match string, case insensitive
Given a sample.yml file of:
foo bar FOOthen
yq '[match("(?i)foo"; "g")]' sample.ymlwill output
- string: foo
offset: 0
length: 3
captures: []
- string: FOO
offset: 8
length: 3
captures: []Match with global capture group
Given a sample.yml file of:
abc abcthen
yq '[match("(ab)(c)"; "g")]' sample.ymlwill output
- string: abc
offset: 0
length: 3
captures:
- string: ab
offset: 0
length: 2
- string: c
offset: 2
length: 1
- string: abc
offset: 4
length: 3
captures:
- string: ab
offset: 4
length: 2
- string: c
offset: 6
length: 1Match with named capture groups
Given a sample.yml file of:
foo bar foo foo foothen
yq '[match("foo (?P<bar123>bar)? foo"; "g")]' sample.ymlwill output
- string: foo bar foo
offset: 0
length: 11
captures:
- string: bar
offset: 4
length: 3
name: bar123
- string: foo foo
offset: 12
length: 8
captures:
- string: null
offset: -1
length: 0
name: bar123Capture named groups into a map
Given a sample.yml file of:
xyzzy-14then
yq 'capture("(?P<a>[a-z]+)-(?P<n>[0-9]+)")' sample.ymlwill output
a: xyzzy
n: "14"Match without global flag
Given a sample.yml file of:
cat catthen
yq 'match("cat")' sample.ymlwill output
string: cat
offset: 0
length: 3
captures: []Match with global flag
Given a sample.yml file of:
cat catthen
yq '[match("cat"; "g")]' sample.ymlwill output
- string: cat
offset: 0
length: 3
captures: []
- string: cat
offset: 4
length: 3
captures: []Test using regex
Like jq's equivalent, this works like match but only returns true/false instead of full match details
Given a sample.yml file of:
- cat
- dogthen
yq '.[] | test("at")' sample.ymlwill output
true
falseSubstitute / Replace string
This uses Golang's regex, described here.
Note the use of |= to run in context of the current string value.
Given a sample.yml file of:
a: dogs are greatthen
yq '.a |= sub("dogs", "cats")' sample.ymlwill output
a: cats are greatSubstitute / Replace string with regex
This uses Golang's regex, described here.
Note the use of |= to run in context of the current string value.
Given a sample.yml file of:
a: cat
b: heatthen
yq '.[] |= sub("(a)", "${1}r")' sample.ymlwill output
a: cart
b: heartCustom types: that are really strings
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
a: !horse cat
b: !goat heatthen
yq '.[] |= sub("(a)", "${1}r")' sample.ymlwill output
a: !horse cart
b: !goat heartSplit strings
Given a sample.yml file of:
cat; meow; 1; ; truethen
yq 'split("; ")' sample.ymlwill output
- cat
- meow
- "1"
- ""
- "true"Split strings one match
Given a sample.yml file of:
wordthen
yq 'split("; ")' sample.ymlwill output
- wordTo string
Note that you may want to force yq to leave scalar values wrapped by passing in --unwrapScalar=false or -r=f
Given a sample.yml file of:
- 1
- true
- null
- ~
- cat
- an: object
- - array
- 2then
yq '.[] |= to_string' sample.ymlwill output
- "1"
- "true"
- "null"
- "~"
- cat
- "an: object"
- "- array\n- 2"Last updated
Was this helpful?