yq
v4.x
v4.x
  • yq
  • How It Works
  • Recipes
  • Upgrading from V3
  • Commands
    • Evaluate
    • Evaluate All
    • Shell Completion
  • Operators
    • Add
    • Alternative (Default value)
    • Anchor and Alias Operators
    • Array to Map
    • Assign (Update)
    • Boolean Operators
    • Collect into Array
    • Column
    • Comment Operators
    • Compare Operators
    • Contains
    • Create, Collect into Object
    • Date Time
    • Delete
    • Divide
    • Document Index
    • Encode / Decode
    • Entries
    • Env Variable Operators
    • Equals
    • Eval
    • File Operators
    • Filter Operator
    • Flatten
    • Group By
    • Has
    • Keys
    • Kind
    • Length
    • Line
    • Load
    • Min
    • Map
    • Max
    • Modulo
    • Multiply (Merge)
    • Omit
    • Parent
    • Path
    • Pick
    • Pipe
    • Pivot
    • Recursive Descent (Glob)
    • Reduce
    • Reverse
    • Select
    • Shuffle
    • Slice Array
    • Sort
    • Sort Keys
    • Split into Documents
    • String Operators
    • Style
    • Subtract
    • Tag
    • To Number
    • Traverse (Read)
    • Union
    • Unique
    • Variable Operators
    • With
  • Usage
    • Output format
    • Working with CSV, TSV
    • Working with JSON
    • Working with Properties
    • Working with XML
    • Working with LUA
    • Working with TOML
    • Working with Shell Output
    • Front Matter
    • Split into multiple files
    • GitHub Action
    • Tips, Tricks, Troubleshooting
  • Github Page
Powered by GitBook
On this page
  • Split documents into files
  • Split documents into files, using index
  • Split single document into files

Was this helpful?

  1. Usage

Split into multiple files

yq can split out the results into multiple files with the --split-exp/s flag. You will need to give this flag an expression (that returns a string), this will be used as the filename for each result. In this expression, you can use $index to represent the result index in the name, if desired.

Split documents into files

Given a file like

a: test_doc1
--- 
a: test_doc2

Then running:

yq -s '.a' myfile.yml

will result in two files:

test_doc1.yml:

a: test_doc1

test_doc2.yml:

---
a: test_doc2

TIP: if you don't want the leading document separators (---), then run with the --no-doc flag.

Split documents into files, using index

This is like the example above, but we'll use $index for the filename. Note that this variable is only defined for the --split-exp/s flag.

yq -s '"file_" + $index' myfile.yml

This will create two files, file_0.yml and file_1.yml.

Split single document into files

You can also split results into separate files. Notice

- name: bob
  age: 23
- name: tim
  age: 17

Then, by splatting the array into individual results, we can split the content into several files:

yq '.[]' file.yml -s '"user_" + .name'

will result in two files:

user_bob.yml:

name: bob
age: 23

user_tim.yml:

name: tim
age: 17
PreviousFront MatterNextGitHub Action

Last updated 3 years ago

Was this helpful?