mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
1 line
24 KiB
JSON
1 line
24 KiB
JSON
{"config":{"lang":["en"],"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"yq yq is a lightweight and portable command-line YAML processor The aim of the project is to be the jq or sed of yaml files. Install On MacOS: brew install yq On Ubuntu and other Linux distros supporting snap packages: snap install yq On Ubuntu 16.04 or higher from Debian package: sudo add-apt-repository ppa:rmescandon/yq sudo apt update sudo apt install yq -y or, Download latest binary or alternatively: go get gopkg.in/mikefarah/yq.v2 View on GitHub","title":"Install"},{"location":"#yq","text":"yq is a lightweight and portable command-line YAML processor The aim of the project is to be the jq or sed of yaml files.","title":"yq"},{"location":"#install","text":"On MacOS: brew install yq On Ubuntu and other Linux distros supporting snap packages: snap install yq On Ubuntu 16.04 or higher from Debian package: sudo add-apt-repository ppa:rmescandon/yq sudo apt update sudo apt install yq -y or, Download latest binary or alternatively: go get gopkg.in/mikefarah/yq.v2 View on GitHub","title":"Install"},{"location":"convert/","text":"Yaml to Json To convert output to json, use the --tojson (or -j) flag. This can only be used with the read command. Given a sample.yaml file of: b: c: 2 then yq r -j sample.yaml b.c will output { b :{ c :2}} Json to Yaml To read in json, just pass in a json file instead of yaml, it will just work :) e.g given a json file { a : Easy! as one two three , b :{ c :2, d :[3,4]}} then yq r sample.json will output a: Easy! as one two three b: c: 2 d: - 3 - 4","title":"Convert"},{"location":"convert/#yaml-to-json","text":"To convert output to json, use the --tojson (or -j) flag. This can only be used with the read command. Given a sample.yaml file of: b: c: 2 then yq r -j sample.yaml b.c will output { b :{ c :2}}","title":"Yaml to Json"},{"location":"convert/#json-to-yaml","text":"To read in json, just pass in a json file instead of yaml, it will just work :) e.g given a json file { a : Easy! as one two three , b :{ c :2, d :[3,4]}} then yq r sample.json will output a: Easy! as one two three b: c: 2 d: - 3 - 4","title":"Json to Yaml"},{"location":"create/","text":"Yaml files can be created using the 'new' command. This works in the same way as the write command, but you don't pass in an existing Yaml file. Currently this does not support creating multiple documents in a single yaml file. yq n path new value Creating a simple yaml file yq n b.c cat will output: b: c: cat Creating using a create script Create scripts follow the same format as the update scripts. Given a script create_instructions.yaml of: b.c: 3 b.e[0].name: Howdy Partner then yq n -s create_instructions.yaml will output: b: c: 3 e: - name: Howdy Partner You can also pipe the instructions in: cat create_instructions.yaml | yq n -s - Keys with dots When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Create"},{"location":"create/#creating-a-simple-yaml-file","text":"yq n b.c cat will output: b: c: cat","title":"Creating a simple yaml file"},{"location":"create/#creating-using-a-create-script","text":"Create scripts follow the same format as the update scripts. Given a script create_instructions.yaml of: b.c: 3 b.e[0].name: Howdy Partner then yq n -s create_instructions.yaml will output: b: c: 3 e: - name: Howdy Partner You can also pipe the instructions in: cat create_instructions.yaml | yq n -s -","title":"Creating using a create script"},{"location":"create/#keys-with-dots","text":"When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Keys with dots"},{"location":"delete/","text":"yq d yaml_file path_to_delete To Stdout Given a sample.yaml file of: b: c: 2 apples: green then yq d sample.yaml b.c will output: b: apples: green From STDIN cat sample.yaml | yq d - b.c Deleting array elements Given a sample.yaml file of: b: c: - 1 - 2 - 3 then yq d sample.yaml 'b.c[1]' will output: b: c: - 1 - 3 Deleting nodes in-place Given a sample.yaml file of: b: c: 2 apples: green then yq d -i sample.yaml b.c will update the sample.yaml file so that the 'c' node is deleted Multiple Documents - delete from single document Given a sample.yaml file of: something: else field: leaveMe --- b: c: 2 field: deleteMe then yq w -d1 sample.yaml field will output: something: else field: leaveMe --- b: c: 2 Multiple Documents - delete from all documents Given a sample.yaml file of: something: else field: deleteMe --- b: c: 2 field: deleteMeToo then yq w -d'*' sample.yaml field will output: something: else --- b: c: 2 Note that '*' is in quotes to avoid being interpreted by your shell. Keys with dots When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Delete"},{"location":"delete/#to-stdout","text":"Given a sample.yaml file of: b: c: 2 apples: green then yq d sample.yaml b.c will output: b: apples: green","title":"To Stdout"},{"location":"delete/#from-stdin","text":"cat sample.yaml | yq d - b.c","title":"From STDIN"},{"location":"delete/#deleting-array-elements","text":"Given a sample.yaml file of: b: c: - 1 - 2 - 3 then yq d sample.yaml 'b.c[1]' will output: b: c: - 1 - 3","title":"Deleting array elements"},{"location":"delete/#deleting-nodes-in-place","text":"Given a sample.yaml file of: b: c: 2 apples: green then yq d -i sample.yaml b.c will update the sample.yaml file so that the 'c' node is deleted","title":"Deleting nodes in-place"},{"location":"delete/#multiple-documents-delete-from-single-document","text":"Given a sample.yaml file of: something: else field: leaveMe --- b: c: 2 field: deleteMe then yq w -d1 sample.yaml field will output: something: else field: leaveMe --- b: c: 2","title":"Multiple Documents - delete from single document"},{"location":"delete/#multiple-documents-delete-from-all-documents","text":"Given a sample.yaml file of: something: else field: deleteMe --- b: c: 2 field: deleteMeToo then yq w -d'*' sample.yaml field will output: something: else --- b: c: 2 Note that '*' is in quotes to avoid being interpreted by your shell.","title":"Multiple Documents - delete from all documents"},{"location":"delete/#keys-with-dots","text":"When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Keys with dots"},{"location":"merge/","text":"Yaml files can be merged using the 'merge' command. Each additional file merged with the first file will set values for any key not existing already or where the key has no value. yq m yaml_file path ... To Stdout Given a data1.yaml file of: a: simple b: [1, 2] and data2.yaml file of: a: other c: test: 1 then yq m data1.yaml data2.yaml will output: a: simple b: [1, 2] c: test: 1 Updating files in-place Given a data1.yaml file of: a: simple b: [1, 2] and data2.yaml file of: a: other c: test: 1 then yq m -i data1.yaml data2.yaml will update the data1.yaml file so that the value of 'c' is 'test: 1'. Overwrite values Given a data1.yaml file of: a: simple b: [1, 2] and data2.yaml file of: a: other c: test: 1 then yq m -x data1.yaml data2.yaml will output: a: other b: [1, 2] c: test: 1 Overwrite values with arrays Given a data1.yaml file of: a: simple b: [1, 2] and data3.yaml file of: b: [3, 4] c: test: 2 other: true d: false then yq m -x data1.yaml data3.yaml will output: a: simple b: [3, 4] c: test: 2 other: true d: false Notice that 'b' does not result in the merging of the values within an array. Append values with arrays Given a data1.yaml file of: a: simple b: [1, 2] d: hi and data3.yaml file of: a: something b: [3, 4] c: test: 2 other: true then yq m -a data1.yaml data3.yaml will output: a: simple b: [1, 2, 3, 4] c: test: 2 other: true d: hi Note that the 'b' array has concatenated the values from the second data file. Also note that other map keys are not overridden (field a). Append cannot be used with overwrite, if both flags are given then append is ignored. Multiple Documents - merge into single document Currently yq only has multi-document support for the first document being merged into. The remaining yaml files will have their first document selected. Given a data1.yaml file of: something: else --- a: simple b: cat and data3.yaml file of: b: dog then yq m -x -d1 data1.yaml data3.yaml will output: something: else --- a: simple b: dog Multiple Documents - merge into all documents Currently yq only has multi-document support for the first document being merged into. The remaining yaml files will have their first document selected. Given a data1.yaml file of: something: else --- a: simple b: cat and data3.yaml file of: b: dog then yq m -x -d'*' data1.yaml data3.yaml will output: b: dog something: else --- a: simple b: dog","title":"Merge"},{"location":"merge/#to-stdout","text":"Given a data1.yaml file of: a: simple b: [1, 2] and data2.yaml file of: a: other c: test: 1 then yq m data1.yaml data2.yaml will output: a: simple b: [1, 2] c: test: 1","title":"To Stdout"},{"location":"merge/#updating-files-in-place","text":"Given a data1.yaml file of: a: simple b: [1, 2] and data2.yaml file of: a: other c: test: 1 then yq m -i data1.yaml data2.yaml will update the data1.yaml file so that the value of 'c' is 'test: 1'.","title":"Updating files in-place"},{"location":"merge/#overwrite-values","text":"Given a data1.yaml file of: a: simple b: [1, 2] and data2.yaml file of: a: other c: test: 1 then yq m -x data1.yaml data2.yaml will output: a: other b: [1, 2] c: test: 1","title":"Overwrite values"},{"location":"merge/#overwrite-values-with-arrays","text":"Given a data1.yaml file of: a: simple b: [1, 2] and data3.yaml file of: b: [3, 4] c: test: 2 other: true d: false then yq m -x data1.yaml data3.yaml will output: a: simple b: [3, 4] c: test: 2 other: true d: false Notice that 'b' does not result in the merging of the values within an array.","title":"Overwrite values with arrays"},{"location":"merge/#append-values-with-arrays","text":"Given a data1.yaml file of: a: simple b: [1, 2] d: hi and data3.yaml file of: a: something b: [3, 4] c: test: 2 other: true then yq m -a data1.yaml data3.yaml will output: a: simple b: [1, 2, 3, 4] c: test: 2 other: true d: hi Note that the 'b' array has concatenated the values from the second data file. Also note that other map keys are not overridden (field a). Append cannot be used with overwrite, if both flags are given then append is ignored.","title":"Append values with arrays"},{"location":"merge/#multiple-documents-merge-into-single-document","text":"Currently yq only has multi-document support for the first document being merged into. The remaining yaml files will have their first document selected. Given a data1.yaml file of: something: else --- a: simple b: cat and data3.yaml file of: b: dog then yq m -x -d1 data1.yaml data3.yaml will output: something: else --- a: simple b: dog","title":"Multiple Documents - merge into single document"},{"location":"merge/#multiple-documents-merge-into-all-documents","text":"Currently yq only has multi-document support for the first document being merged into. The remaining yaml files will have their first document selected. Given a data1.yaml file of: something: else --- a: simple b: cat and data3.yaml file of: b: dog then yq m -x -d'*' data1.yaml data3.yaml will output: b: dog something: else --- a: simple b: dog","title":"Multiple Documents - merge into all documents"},{"location":"prefix/","text":"Paths can be prefixed using the 'prefix' command. The complete yaml content will be nested inside the new prefix path. yq p yaml_file path To Stdout Given a data1.yaml file of: a: simple b: [1, 2] then yq p data1.yaml c will output: c: a: simple b: [1, 2] Arbitrary depth Given a data1.yaml file of: a: b: [1, 2] then yq p data1.yaml c.d will output: c: d: a: b: [1, 2] Updating files in-place Given a data1.yaml file of: a: simple b: [1, 2] then yq p -i data1.yaml c will update the data1.yaml file so that the path 'c' is prefixed to all other paths. Multiple Documents - prefix a single document Given a data1.yaml file of: something: else --- a: simple b: cat then yq p -d1 data1.yaml c will output: something: else --- c: a: simple b: cat Multiple Documents - prefix all documents Given a data1.yaml file of: something: else --- a: simple b: cat then yq p -d'*' data1.yaml c will output: c: something: else --- c: a: simple b: cat","title":"Prefix"},{"location":"prefix/#to-stdout","text":"Given a data1.yaml file of: a: simple b: [1, 2] then yq p data1.yaml c will output: c: a: simple b: [1, 2]","title":"To Stdout"},{"location":"prefix/#arbitrary-depth","text":"Given a data1.yaml file of: a: b: [1, 2] then yq p data1.yaml c.d will output: c: d: a: b: [1, 2]","title":"Arbitrary depth"},{"location":"prefix/#updating-files-in-place","text":"Given a data1.yaml file of: a: simple b: [1, 2] then yq p -i data1.yaml c will update the data1.yaml file so that the path 'c' is prefixed to all other paths.","title":"Updating files in-place"},{"location":"prefix/#multiple-documents-prefix-a-single-document","text":"Given a data1.yaml file of: something: else --- a: simple b: cat then yq p -d1 data1.yaml c will output: something: else --- c: a: simple b: cat","title":"Multiple Documents - prefix a single document"},{"location":"prefix/#multiple-documents-prefix-all-documents","text":"Given a data1.yaml file of: something: else --- a: simple b: cat then yq p -d'*' data1.yaml c will output: c: something: else --- c: a: simple b: cat","title":"Multiple Documents - prefix all documents"},{"location":"read/","text":"yq r yaml_file|json_file path This command can take a json file as input too, and will output yaml unless specified to export as json (-j) Basic Given a sample.yaml file of: b: c: 2 then yq r sample.yaml b.c will output the value of '2'. From Stdin Given a sample.yaml file of: cat sample.yaml | yq r - b.c will output the value of '2'. Splat Given a sample.yaml file of: --- bob: item1: cats: bananas item2: cats: apples then yq r sample.yaml bob.*.cats will output - bananas - apples Multiple Documents - specify a single document Given a sample.yaml file of: something: else --- b: c: 2 then yq r -d1 sample.yaml b.c will output the value of '2'. Multiple Documents - read all documents Reading all documents will return the result as an array. This can be converted to json using the '-j' flag if desired. Given a sample.yaml file of: name: Fred age: 22 --- name: Stella age: 23 --- name: Android age: 232 then yq r -d'*' sample.yaml name will output: - Fred - Stella - Android Arrays You can give an index to access a specific element: e.g.: given a sample file of b: e: - name: fred value: 3 - name: sam value: 4 then yq r sample.yaml 'b.e[1].name' will output 'sam' Note that the path is in quotes to avoid the square brackets being interpreted by your shell. Array Splat e.g.: given a sample file of b: e: - name: fred value: 3 - name: sam value: 4 then yq r sample.yaml 'b.e[*].name' will output: - fred - sam Note that the path is in quotes to avoid the square brackets being interpreted by your shell. Keys with dots When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Read"},{"location":"read/#basic","text":"Given a sample.yaml file of: b: c: 2 then yq r sample.yaml b.c will output the value of '2'.","title":"Basic"},{"location":"read/#from-stdin","text":"Given a sample.yaml file of: cat sample.yaml | yq r - b.c will output the value of '2'.","title":"From Stdin"},{"location":"read/#splat","text":"Given a sample.yaml file of: --- bob: item1: cats: bananas item2: cats: apples then yq r sample.yaml bob.*.cats will output - bananas - apples","title":"Splat"},{"location":"read/#multiple-documents-specify-a-single-document","text":"Given a sample.yaml file of: something: else --- b: c: 2 then yq r -d1 sample.yaml b.c will output the value of '2'.","title":"Multiple Documents - specify a single document"},{"location":"read/#multiple-documents-read-all-documents","text":"Reading all documents will return the result as an array. This can be converted to json using the '-j' flag if desired. Given a sample.yaml file of: name: Fred age: 22 --- name: Stella age: 23 --- name: Android age: 232 then yq r -d'*' sample.yaml name will output: - Fred - Stella - Android","title":"Multiple Documents - read all documents"},{"location":"read/#arrays","text":"You can give an index to access a specific element: e.g.: given a sample file of b: e: - name: fred value: 3 - name: sam value: 4 then yq r sample.yaml 'b.e[1].name' will output 'sam' Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Arrays"},{"location":"read/#array-splat","text":"e.g.: given a sample file of b: e: - name: fred value: 3 - name: sam value: 4 then yq r sample.yaml 'b.e[*].name' will output: - fred - sam Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Array Splat"},{"location":"read/#keys-with-dots","text":"When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Keys with dots"},{"location":"write/","text":"yq w yaml_file path new value To Stdout Given a sample.yaml file of: b: c: 2 then yq w sample.yaml b.c cat will output: b: c: cat From STDIN cat sample.yaml | yq w - b.c blah Adding new fields Any missing fields in the path will be created on the fly. Given a sample.yaml file of: b: c: 2 then yq w sample.yaml b.d[0] new thing will output: b: c: cat d: - new thing Appending value to an array field Given a sample.yaml file of: b: c: 2 d: - new thing - foo thing then yq w sample.yaml b.d[+] bar thing will output: b: c: cat d: - new thing - foo thing - bar thing Note that the path is in quotes to avoid the square brackets being interpreted by your shell. Multiple Documents - update a single document Given a sample.yaml file of: something: else --- b: c: 2 then yq w -d1 sample.yaml b.c 5 will output: something: else --- b: c: 5 Multiple Documents - update all documents Given a sample.yaml file of: something: else --- b: c: 2 then yq w -d'*' sample.yaml b.c 5 will output: something: else b: c: 5 --- b: c: 5 Note that '*' is in quotes to avoid being interpreted by your shell. Updating files in-place Given a sample.yaml file of: b: c: 2 then yq w -i sample.yaml b.c cat will update the sample.yaml file so that the value of 'c' is cat. Updating multiple values with a script Given a sample.yaml file of: b: c: 2 e: - name: Billy Bob and a script update_instructions.yaml of: b.c: 3 b.e[0].name: Howdy Partner then yq w -s update_instructions.yaml sample.yaml will output: b: c: 3 e: - name: Howdy Partner And, of course, you can pipe the instructions in using '-': cat update_instructions.yaml | yq w -s - sample.yaml Values starting with a hyphen (or dash) The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags: yq w -- my.path -3 will output my: path: -3 Keys with dots When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Write/Update"},{"location":"write/#to-stdout","text":"Given a sample.yaml file of: b: c: 2 then yq w sample.yaml b.c cat will output: b: c: cat","title":"To Stdout"},{"location":"write/#from-stdin","text":"cat sample.yaml | yq w - b.c blah","title":"From STDIN"},{"location":"write/#adding-new-fields","text":"Any missing fields in the path will be created on the fly. Given a sample.yaml file of: b: c: 2 then yq w sample.yaml b.d[0] new thing will output: b: c: cat d: - new thing","title":"Adding new fields"},{"location":"write/#appending-value-to-an-array-field","text":"Given a sample.yaml file of: b: c: 2 d: - new thing - foo thing then yq w sample.yaml b.d[+] bar thing will output: b: c: cat d: - new thing - foo thing - bar thing Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Appending value to an array field"},{"location":"write/#multiple-documents-update-a-single-document","text":"Given a sample.yaml file of: something: else --- b: c: 2 then yq w -d1 sample.yaml b.c 5 will output: something: else --- b: c: 5","title":"Multiple Documents - update a single document"},{"location":"write/#multiple-documents-update-all-documents","text":"Given a sample.yaml file of: something: else --- b: c: 2 then yq w -d'*' sample.yaml b.c 5 will output: something: else b: c: 5 --- b: c: 5 Note that '*' is in quotes to avoid being interpreted by your shell.","title":"Multiple Documents - update all documents"},{"location":"write/#updating-files-in-place","text":"Given a sample.yaml file of: b: c: 2 then yq w -i sample.yaml b.c cat will update the sample.yaml file so that the value of 'c' is cat.","title":"Updating files in-place"},{"location":"write/#updating-multiple-values-with-a-script","text":"Given a sample.yaml file of: b: c: 2 e: - name: Billy Bob and a script update_instructions.yaml of: b.c: 3 b.e[0].name: Howdy Partner then yq w -s update_instructions.yaml sample.yaml will output: b: c: 3 e: - name: Howdy Partner And, of course, you can pipe the instructions in using '-': cat update_instructions.yaml | yq w -s - sample.yaml","title":"Updating multiple values with a script"},{"location":"write/#values-starting-with-a-hyphen-or-dash","text":"The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags: yq w -- my.path -3 will output my: path: -3","title":"Values starting with a hyphen (or dash)"},{"location":"write/#keys-with-dots","text":"When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Keys with dots"},{"location":"snippets/keys_with_dots/","text":"Keys with dots When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Keys with dots"},{"location":"snippets/keys_with_dots/#keys-with-dots","text":"When specifying a key that has a dot use key lookup indicator. b: foo.bar: 7 yaml r sample.yaml 'b[foo.bar]' yaml w sample.yaml 'b[foo.bar]' 9 Any valid yaml key can be specified as part of a key lookup. Note that the path is in quotes to avoid the square brackets being interpreted by your shell.","title":"Keys with dots"},{"location":"snippets/works_with_json/","text":"This command can take a json file as input too, and will output yaml unless specified to export as json (-j)","title":"Works with json"}]} |