title: Generate csv file in awk command
body: <p>I am executing below awk command for splitting the files and generate the output in .CSV format. I tried many things to pass the correct syntax in the script to generate on csv file but no luck. Can some please let me know where to mention the correct syntax.</p>
  <pre><code>awk -v lines=&quot;30000&quot; -v pre=&quot;awk_part_&quot; '
          NR==1 { header=$0; next}
          (NR-1) % lines ==1 { fname=pre c++; print header &gt; fname}
          {print &gt; fname}' patient.tsv
  </code></pre>
  <p>How the above command works:</p>
  <ol>
  <li>awk -v lines=”30000″ -v pre=”awk_part_” – First, I’ve declared two awk variables to define how many records are in each split file and the prefix of the filenames</li>
  <li>NR==1 { header=$0; next} – When awk reads the first line, it stores the line in a header variable and stops further processing</li>
  <li>(NR-1) % lines ==1 { fname=pre c++; print header &gt; fname} – When the current line is the first record of a trunk, we need to update the filename (fname) by incrementing a counter (c). Also, since this would be a new file, we add the value of the header variable to the file as the first line</li>
  <li>{print &gt; fname}’ patient.tsv – Then, I've just redirected each record line to the current fname file</li>
  </ol>