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
  • Merging files
  • Get filename
  • Get file index
  • Get file indices of multiple documents
  • Get file index alias

Was this helpful?

  1. Operators

File Operators

File operators are most often used with merge when needing to merge specific files together. Note that when doing this, you will need to use eval-all to ensure all yaml documents are loaded into memory before performing the merge (as opposed to eval which runs the expression once per document).

Note that the fileIndex operator has a short alias of fi.

Merging files

Note the use of eval-all to ensure all documents are loaded into memory.

yq eval-all 'select(fi == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml

Get filename

Given a sample.yml file of:

a: cat

then

yq 'filename' sample.yml

will output

sample.yml

Get file index

Given a sample.yml file of:

a: cat

then

yq 'file_index' sample.yml

will output

0

Get file indices of multiple documents

Given a sample.yml file of:

a: cat

And another sample another.yml file of:

a: cat

then

yq eval-all 'file_index' sample.yml another.yml

will output

0
1

Get file index alias

Given a sample.yml file of:

a: cat

then

yq 'fi' sample.yml

will output

0
PreviousEvalNextFilter Operator

Last updated 1 year ago

Was this helpful?