title: How do I use gawk to convert csv to xml?
body: <p>I want to create an executable file using awk (gawk) on Ubuntu 16.04 to convert a csv file to an xml file.  From what I gathered, the format needed is below.  </p>
  
  <pre><code>$ cat tst.awk
  BEGIN { FS="," }
  NR==1 {
  for (i=1; i&lt;=NF; i++) {
      tags[i] = $i
  }
  next
  }
  {
  print "&lt;whatever&gt;"
  for (i=1; i&lt;=NF; i++) {
      printf "    &lt;%s&gt;%s&lt;/%s&gt;\n", tags[i], $i, tags[i]
  }
  print "&lt;/whatever&gt;"
  }
  </code></pre>
  
  <p>How do I tell awk to insert each field and repeat until the end of the file?</p>
  
  <p>input</p>
  
  <pre><code>$ cat file.csv
  id,color,status
  7,black,open
  52,white,closed
  </code></pre>
  
  <p>output</p>
  
  <pre><code>awk -f tst.awk file.csv
  &lt;whatever&gt;
      &lt;id&gt;7&lt;/id&gt;
      &lt;color&gt;black&lt;/color&gt;
      &lt;status&gt;open&lt;/status&gt;
  &lt;/whatever&gt;
  &lt;whatever&gt;
      &lt;id&gt;52&lt;/id&gt;
      &lt;color&gt;white&lt;/color&gt;
      &lt;status&gt;closed&lt;/status&gt;
  &lt;/whatever&gt;
  </code></pre>
  
  <p>This prints to the screen but I need it in an output file.  I also need to put true if certain field is 1 and false if certain field is 0.</p>
  
  <p>needed output example</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Batch&gt;
    &lt;ASWFileVersion&gt;
    &lt;BatchHeader&gt;
      &lt;BatchId&gt;965&lt;/BatchId&gt;
      &lt;UserBatchId&gt;965&lt;/UserBatchId&gt;
    &lt;/BatchHeader&gt;
    &lt;BatchCustomer&gt;
       &lt;EmployeeNumber&gt;123456&lt;/EmployeeNumber&gt;
       &lt;FirstName&gt;fname&lt;/FirstName&gt;
       &lt;LastName&gt;lname&lt;/LastName&gt;
       &lt;Employee&gt;true &lt;/Employee&gt;
    &lt;/BatchCustomer&gt;
    &lt;BatchCustomer&gt;
       &lt;EmployeeNumber&gt;654321&lt;/EmployeeNumber&gt;
       &lt;FirstName&gt;first&lt;/FirstName&gt;
       &lt;LastName&gt;lastn&lt;/LastName&gt;
       &lt;Employee&gt;false&lt;/Employee&gt;
    &lt;/BatchCustomer&gt;
  &lt;/Batch&gt;
  </code></pre>
  
  <p>There is no header to the file and fields are not in order they must appear in xml.  There is a boolean 1 or 0 value that must be translated to true or false.</p>