mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-01 14:14:52 +08:00
Update document generation script
This commit is contained in:
+20
-100
@@ -1,13 +1,10 @@
|
||||
# String Operators
|
||||
|
||||
## String Operators
|
||||
## RegEx
|
||||
This uses golangs native regex functions under the hood - See https://github.com/google/re2/wiki/Syntax for the supported syntax.
|
||||
|
||||
### RegEx
|
||||
|
||||
This uses golangs native regex functions under the hood - See [https://github.com/google/re2/wiki/Syntax](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:
|
||||
@@ -25,7 +22,6 @@ a: cat
|
||||
```
|
||||
|
||||
However, using printf works:
|
||||
|
||||
```
|
||||
printf -v m "cat\n" ; m="$m" yq e -n '.a = strenv(m)'
|
||||
a: |
|
||||
@@ -33,7 +29,6 @@ a: |
|
||||
```
|
||||
|
||||
As well as having multiline expressions:
|
||||
|
||||
```
|
||||
m="cat
|
||||
" yq e -n '.a = strenv(m)'
|
||||
@@ -48,10 +43,8 @@ IFS= read -rd '' output < <(cat my_file)
|
||||
output=$output ./yq e '.data.values = strenv(output)' first.yml
|
||||
```
|
||||
|
||||
### Join strings
|
||||
|
||||
## Join strings
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
- cat
|
||||
- meow
|
||||
@@ -59,35 +52,25 @@ Given a sample.yml file of:
|
||||
- null
|
||||
- true
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval 'join("; ")' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
cat; meow; 1; ; true
|
||||
```
|
||||
|
||||
### Match string
|
||||
|
||||
## Match string
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
foo bar foo
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval 'match("foo")' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
string: foo
|
||||
offset: 0
|
||||
@@ -95,22 +78,16 @@ length: 3
|
||||
captures: []
|
||||
```
|
||||
|
||||
### Match string, case insensitive
|
||||
|
||||
## Match string, case insensitive
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
foo bar FOO
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '[match("(?i)foo"; "g")]' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
- string: foo
|
||||
offset: 0
|
||||
@@ -122,22 +99,16 @@ will output
|
||||
captures: []
|
||||
```
|
||||
|
||||
### Match with capture groups
|
||||
|
||||
## Match with capture groups
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
abc abc
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '[match("(abc)+"; "g")]' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
- string: abc
|
||||
offset: 0
|
||||
@@ -155,22 +126,16 @@ will output
|
||||
length: 3
|
||||
```
|
||||
|
||||
### Match with named capture groups
|
||||
|
||||
## Match with named capture groups
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
foo bar foo foo foo
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '[match("foo (?P<bar123>bar)? foo"; "g")]' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
- string: foo bar foo
|
||||
offset: 0
|
||||
@@ -190,43 +155,31 @@ will output
|
||||
name: bar123
|
||||
```
|
||||
|
||||
### Capture named groups into a map
|
||||
|
||||
## Capture named groups into a map
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
xyzzy-14
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval 'capture("(?P<a>[a-z]+)-(?P<n>[0-9]+)")' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
a: xyzzy
|
||||
n: "14"
|
||||
```
|
||||
|
||||
### Match without global flag
|
||||
|
||||
## Match without global flag
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
cat cat
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval 'match("cat")' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
string: cat
|
||||
offset: 0
|
||||
@@ -234,22 +187,16 @@ length: 3
|
||||
captures: []
|
||||
```
|
||||
|
||||
### Match with global flag
|
||||
|
||||
## Match with global flag
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
cat cat
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '[match("cat"; "g")]' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
- string: cat
|
||||
offset: 0
|
||||
@@ -261,92 +208,70 @@ will output
|
||||
captures: []
|
||||
```
|
||||
|
||||
### Test using regex
|
||||
|
||||
## Test using regex
|
||||
Like jq'q equivalant, this works like match but only returns true/false instead of full match details
|
||||
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
- cat
|
||||
- dog
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '.[] | test("at")' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
true
|
||||
false
|
||||
```
|
||||
|
||||
### Substitute / Replace string
|
||||
|
||||
This uses golang regex, described [here](https://github.com/google/re2/wiki/Syntax) Note the use of `|=` to run in context of the current string value.
|
||||
## Substitute / Replace string
|
||||
This uses golang regex, described [here](https://github.com/google/re2/wiki/Syntax)
|
||||
Note the use of `|=` to run in context of the current string value.
|
||||
|
||||
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) Note the use of `|=` to run in context of the current string value.
|
||||
## Substitute / Replace string with regex
|
||||
This uses golang regex, described [here](https://github.com/google/re2/wiki/Syntax)
|
||||
Note the use of `|=` to run in context of the current string value.
|
||||
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
a: cat
|
||||
b: heat
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '.[] |= sub("(a)", "${1}r")' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
a: cart
|
||||
b: heart
|
||||
```
|
||||
|
||||
### Split strings
|
||||
|
||||
## 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
|
||||
@@ -355,22 +280,17 @@ will output
|
||||
- "true"
|
||||
```
|
||||
|
||||
### Split strings one match
|
||||
|
||||
## Split strings one match
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
word
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval 'split("; ")' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
- word
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user