<p>Below is my <code>testfile.yaml</code>:</p>
<pre><code>---
kind: Pod
metadata:
  name: amazing-application
---
kind: Deployment
metadata:
  name: amazing-deployment
---
kind: Service
metadata:
  name: amazing-deployment
---
kind: Service
metadata:
  name: tea-service
</code></pre>
<p>My goal is to split this into 4 files where the filename is <code>.metadata.name</code> and the dir that file goes into is <code>.kind</code>.</p>
<p>I have achieved what I want with this:</p>
<pre><code>for kind in $(yq e '.kind' testfile.yaml | awk '!/^(---)/' | uniq); 
do 
  mkdir &quot;$kind&quot;
  cd &quot;$kind&quot;
  yq 'select(.kind == &quot;'$kind'&quot;)' ../testfile.yaml | yq -s '.metadata.name' 
  cd ..;
done
</code></pre>
<p>What I want to know is how to get a unique together mapping, or somehow using multple criteria to split the testfile rather than through the loop.</p>
<p>Is there a way to use <code>yq</code> and <code>-s</code> or <code>select</code> to select where kind and metadata.name are unique together in that individual document (document as in separated by '---')?</p>
<p>Because if you do <code>yq -s '.kind' testfile.yaml</code> it will yield three yaml files, not four. Same for <code>yq -s '.metadata.name' testfile.yaml</code>; we get three files as not all <code>name</code> are unique - one gets lost.</p>

<p>According to your shell-scripted workaround, you want to store each document into its own file named after <code>.metadata.name</code> in a subdirectory named after <code>.kind</code>. However, yq's <a href="https://mikefarah.gitbook.io/yq/usage/split-into-multiple-files#split-single-document-into-files" rel="nofollow noreferrer">split option</a> <code>-s</code> cannot create subdirectories, and would fail when provided with naming schemes such as <code>-s '.kind + &quot;/&quot; + .metadata.name'</code>.</p>
<p>Alternatives:</p>
<ul>
<li>You can try submitting a <a href="https://github.com/mikefarah/yq/issues" rel="nofollow noreferrer">feature request</a>. The author is quite open for adaptations towards new use-cases. In fact, the <code>-s</code> option by itself also came to life <a href="https://github.com/mikefarah/yq/issues/966" rel="nofollow noreferrer">this way</a>.</li>
<li>You could (temporarily) replace the <code>/</code> character from above for something else (valid but not contained in the names otherwise), and then with a shell script just iterate over the files moving and renaming them into the right places (altogether resulting in just one call to <code>yq</code>). Example using <code>_</code> instead:
<pre class="lang-bash prettyprint-override"><code>yq -s '.kind + &quot;_&quot; + .metadata.name' . testfile.yaml &amp;&amp; for f in *_*
do mkdir -p &quot;${f%%_*}&quot; &amp;&amp; mv &quot;$f&quot; &quot;${f%%_*}/${f#*_}&quot;; done
</code></pre>
</li>
<li>Depending on your overall goal with the files/documents, you could consider  working with copies of the original file, each reduced to just those documents &quot;belonging together&quot;, i.e. replacing the subdirectories with files, and keeping a grouped set of documents within those files. Example using the <code>eval-all</code> (or <code>ea</code>) <a href="https://mikefarah.gitbook.io/yq/commands/evaluate-all" rel="nofollow noreferrer">command</a> in combination with <code>[.]</code> to generate an array of documents, then <code>group_by</code> on <code>.kind</code> to collect into sub-arrays those that share the criteria given, and finally <code>.[] | split_doc</code> to turn those groups into documents again, in order to be captured by <code>-s</code>.
<pre class="lang-bash prettyprint-override"><code>yq ea -s '.[0].kind' '[.] | group_by(.kind) | .[] | split_doc' testfile.yaml
</code></pre>
Given your example, here you'd intentionally get only three files, but with one containing two documents, and two containing one. Note that technically the outputs are still just arrays of (former) documents, not &quot;real&quot; multi-document files, They'd need another <code>.[] | split_doc</code> applied to each one of them in order to properly re-convert them as such (not covered here).</li>
</ul>
