title: Awk command in paragraph mode but skipping blank lines
body: <p>I have one file with several elements <code>&lt;elem&gt;...&lt;/elem&gt;</code>. I need to split this file into <code>n</code> files with <code>m</code> elements each one (argument passed to awk command I am using). For example if my original file has 40 elements, I would want to split in 3 files (10 elements, 13 elements and 17 elements).</p>
  
  <p>The problem is that the original file has elements with different structures.</p>
  
  <pre><code>EDITED AFTER fedorqui comment:
  I use as awk command as files I want to get at the end of the process. 
  That means If I need 3 files with m1, m2 and m3 elements, I will 
  execute 3 awk with different parameters
  </code></pre>
  
  <p><strong>Example of input</strong> (file.txt) (5 elements)</p>
  
  <pre><code>&lt;elem&gt;aaaaaaaa1&lt;/elem&gt;
  &lt;elem&gt;aaaaaaaa2&lt;/elem&gt;
  &lt;elem&gt;bbbbbbbb
  bbbbbbbbb
  bbbbbbbbb&lt;/elem&gt;
  &lt;elem&gt;bbbbbbbb2&lt;/elem&gt;
  &lt;elem&gt;ccccc
  
  cccc&lt;/elem&gt;
  </code></pre>
  
  <p>As you can see, 1st/2nd/4th element is in one line, 3rd element is in 3 lines without blank lines and 5h element is in 3 lines with an blank line.</p>
  
  <blockquote>
    <p><strong><em>Blank lines between elements is not a problem but blank lines inside an element fails</em></strong></p>
  </blockquote>
  
  <p><strong>Example of desired output:</strong></p>
  
  <p>file_1.txt (2 elements)</p>
  
  <pre><code>&lt;elem&gt;aaaaaaaa1&lt;/elem&gt;
  &lt;elem&gt;aaaaaaaa2&lt;/elem&gt;
  </code></pre>
  
  <p>file_2.txt (2 elements)</p>
  
  <pre><code>&lt;elem&gt;bbbbbbbb
  bbbbbbbbb
  bbbbbbbbb&lt;/elem&gt;
  &lt;elem&gt;bbbbbbbb2&lt;/elem&gt;
  </code></pre>
  
  <p>file_3.txt (1 element)</p>
  
  <pre><code>&lt;elem&gt;ccccc
  
  cccc&lt;/elem&gt;
  </code></pre>
  
  <p><strong>AWK command</strong></p>
  
  <p>(suffixFile is the suffix number of the file. For example fileAux_1.txt, fileAux_2.txt...)</p>
  
  <p><em>Attempt1</em></p>
  
  <pre><code>awk -v numElems=$1 -v suffixFile=$2 '{
      for(i=1;i&lt;=numElems;i++) {
          printf "&lt;doc&gt;"$i &gt; "fileAux_" suffixFile".txt"
      }
  }' RS='' FS='&lt;doc&gt;' file.txt
  </code></pre>
  
  <p>Works except for blank lines inside an element. I understand why it fails, because RS='' tells awk to split by blank lines</p>
  
  <p><em>Attempt 2</em></p>
  
  <pre><code>awk -v numElems=$1 -v suffixFile=$2 '{
      for(i=1;i&lt;=numElems;i++) {
          printf $i &gt; "fileAux_" suffixFile".txt"
      }
  }' RS='&lt;doc&gt;' FS='&lt;doc&gt;' file.txt
  </code></pre>
  
  <p>Another aproach but it also fails</p>
  
  <p>¿Can anyone help me?</p>
  
  <p>Thanks in advance!</p>