title: awk CSV Split with headers Windows
body: <p>Ok I have a csv file I need to split based on a column value which is fine, but I cannot get the headers to print in each file.</p>
  
  <p>Currently I use:</p>
  
  <pre><code>awk "FS =\",\" {output=$3\".csv\"; print $0 &gt; output}" test.csv
  </code></pre>
  
  <p>Which splits the files file based on column 3, but I don't know how to add the header to each file.</p>
  
  <p>I've searched high &amp; low but can't find a solution that will work in a one liner...</p>
  
  <h2>UPDATE</h2>
  
  <p>OK to date we have a working one liner:</p>
  
  <pre><code>awk -F, "NR==1{hdr=$0;next}!($3 in files){files[$3]=1;print hdr&gt;$3\".csv\"}{print&gt;$3\".csv\"}" test.csv
  </code></pre>
  
  <p>Or in test.awk:</p>
  
  <pre><code>BEGIN{FS=","} NR==1 {hdr=$0;next}!($3 in files) {files[$3]=1;print hdr&gt;$3".csv"}{print&gt;$3".csv"}
  </code></pre>
  
  <p>Command to run used:</p>
  
  <pre><code>awk -f test.awk test.csv
  </code></pre>
  
  <p>I really appreciate the help here, I've been trying for hours and have a few things left to work out.</p>
  
  <p>1) Blank line inserted after header
  2) Sort the data on specified fields</p>
  
  <p>Further down the line I want to additionally do a row count &amp; cut a reference number from another file is this possible with AWK or am I using the wrong tool for the job?</p>
  
  <p>Thanks again.</p>