title: Split one file into multiple files based on pattern (cut can occur within lines)
body: <p>A lot of solutions exist, but the specificity here is I need to be able to split within a line, the cut should occur just before the pattern. Ex:</p>
  
  <p>Infile:</p>
  
  <pre><code>&lt;?xml 1&gt;&lt;blabla1&gt;
  &lt;blabla&gt;&lt;blabla2&gt;&lt;blabla&gt;
  &lt;blabla&gt;&lt;blabla&gt;
  &lt;blabla&gt;&lt;blabla3&gt;&lt;blabla&gt;&lt;blabla&gt;
  &lt;blabla&gt;&lt;blabla&gt;&lt;blabla&gt;&lt;?xml 4&gt;
  &lt;blabla&gt;
  &lt;blabla&gt;&lt;blabla&gt;&lt;blabla&gt;
  &lt;blabla&gt;&lt;?xml 2&gt;&lt;blabla&gt;&lt;blabla&gt;
  </code></pre>
  
  <p>Should become with pattern <code>&lt;?xml</code></p>
  
  <p>Outfile1:</p>
  
  <pre><code>&lt;?xml 1&gt;&lt;blabla1&gt;
  &lt;blabla&gt;&lt;blabla2&gt;&lt;blabla&gt;
  &lt;blabla&gt;&lt;blabla&gt;
  &lt;blabla&gt;&lt;blabla3&gt;&lt;blabla&gt;&lt;blabla&gt;
  &lt;blabla&gt;&lt;blabla&gt;&lt;blabla&gt;
  </code></pre>
  
  <p>Outfile2:</p>
  
  <pre><code>&lt;?xml 4&gt;
  &lt;blabla&gt;
  &lt;blabla&gt;&lt;blabla&gt;&lt;blabla&gt;
  &lt;blabla&gt;
  </code></pre>
  
  <p>Outfile3:</p>
  
  <pre><code>&lt;?xml 2&gt;&lt;blabla&gt;&lt;blabla&gt;
  </code></pre>
  
  <p>Actually the <code>perl</code> script in the validated answer <a href="https://stackoverflow.com/questions/8061475/split-one-file-into-multiple-files-based-on-pattern">here</a> works fine for my little example. But it generates an error for my bigger (about 6GB) actual files. The error is:</p>
  
  <pre><code>panic: sv_setpvn called with negative strlen at /home/.../split.pl line 7, &lt;&gt; chunk 1.
  </code></pre>
  
  <p>I don't have the permissions to comment, that's why I started a new post.
  And finally, a <code>Python</code> solution would be even more appreciated, as I understand it better.</p>