title: Splitting a file by lines into N guaranteed files as evenly as possible using AWK
body: <p>If I have a file, bigfile.csv which is L+1 lines long (including a header), and my goal is to split this file into N files (say 100) as evenly as possible, appending the header into each of the splits. Assuming that I know how many lines there are in the file, and that how many files it needs to be split into, a priori.</p>
  
  <p>Currently, I already have this code:</p>
  
  <pre><code>awk 'NR==1 {h=$0; next} (NR-2)% 100==0 {filename = sprintf("%04d",int((NR-2)/100)+1) ".smallfile.csv"; print h &gt;&gt; filename} {print &gt;&gt; filename}' bigfile.csv
  </code></pre>
  
  <p>While this code works most of the time, there are two failings with this:</p>
  
  <ol>
  <li>Unless the number of lines in the original file is exactly divisible by N, there will be N-1 files of ceil(L/N) lines with the last file having significantly less than the other files. This is not as big of a deal, however.</li>
  <li>In some situations, where if N is big and the ratio between L and N is low, you wouldn't have N guaranteed files. For example, if the original file has 6021 lines to be split into 100 files, you would only end up with 99 files, since each file would have 61 lines.</li>
  </ol>
  
  <p>How would I do this in AWK? Ideally, given the above example, the split for 6021 lines into 100 files would have 21 files with 61 lines and the rest with 60 lines.</p>
  
  <p>I know that you could use split(1) in unix and do it round-robin style, but that changes the sequence of the lines. Later on these lines would be processed distributively and reassembled, so it would be more ideal to keep the same sequence.</p>