title: how to get the position of nth occurrence of a string in a file
body: <p>I have an xml file which contains data in a single line where same string is repeated multiple times in it.</p>
  <p>I am looking to identify the position of <code>n</code>th occurrence of a string in that file so that i can split single file into multiple files based on that position so that it will be easy for processing.</p>
  <h3>sample data in file:</h3>
  <pre><code>&lt;id = 1&gt;&lt;\id&gt;&lt;id = 2&gt;&lt;\id&gt;&lt;id = 3&gt;&lt;\id&gt;&lt;id = 4&gt;&lt;\id&gt;&lt;id = 5&gt;&lt;\id&gt;&lt;id = 6&gt;&lt;\id&gt;&lt;id = 7&gt;&lt;\id&gt;&lt;id = 8&gt;&lt;\id&gt;&lt;id = 9&gt;&lt;\id&gt;&lt;id = 10&gt;&lt;\id&gt;&lt;id = 11&gt;&lt;\id&gt;
  </code></pre>
  <p>So i want to split the file based on the id tag. for eg i want to look for position of 5th occurrence of the id tag and need to split the file into 3 files totally</p>
  <h3>Output:</h3>
  <h4>file_1:</h4>
  <pre><code>&lt;id = 1&gt;&lt;\id&gt;&lt;id = 2&gt;&lt;\id&gt;&lt;id = 3&gt;&lt;\id&gt;&lt;id = 4&gt;&lt;\id&gt;
  </code></pre>
  <h4>file_2:</h4>
  <pre><code>&lt;id = 5&gt;&lt;\id&gt;&lt;id = 6&gt;&lt;\id&gt;&lt;id = 7&gt;&lt;\id&gt;&lt;id = 8&gt;&lt;\id&gt;
  </code></pre>
  <h4>file_3:</h4>
  <pre><code>&lt;id = 9&gt;&lt;\id&gt;&lt;id = 10&gt;&lt;\id&gt;&lt;id = 11&gt;&lt;\id&gt;
  </code></pre>
  <p>I tried splitting the one line into multiple lines with a simple sed
  <code>sed 's/&gt;&lt;/&gt;\n&lt;/g' $file &gt; data.txt</code>
  Later with a simple grep i identified the line number and started splitting based on the line number. This is working for smaller files but some file are in GB's (10-20) which is causing issues.</p>
  <p>Could you help me if there is any easy way to get the position of the nth occurrence of a string in file so that i can split single file into multiple files based on the string position.</p>