title: Awk to extract and format a highly variable text file
body: <p>I'm dealing with a text file that's just a mess. It's the service record for a used RV that I'm buying, and it's a regex lover's nightmare </p>
  
  <p>It has both inconsistent field separators and an inconsistent number of fields, with the lines being one of two types:</p>
  
  <pre><code>Type 1 (11 columns):
  UNIT   Mile  GnHr  R.O. Ln  Service  Description                Mechanic   Hours  $ Amt
  7-9918;57878 1698 01633 021;0502-00C ENG OIL/ FILTERT IF NEEDED;M02 JOSE A. SANCHEZ;0.80;80.00
  
  Type 2 (10 columns)
  UNIT   Mile  GnHr  R.O. Ln  Service  Description   Hours  $ Amt
  7-9918;55007 1641 [9564 007;ELE-BAT-BAT-0-0AAA;BATTERY AAA ALL BRANDS;2;31.12
  </code></pre>
  
  <p>I've stripped out all the headings, but put them back here just for reference. In Type 2 lines, the Mechanic field is missing.</p>
  
  <p>I replaced all occurrences of multiple spaces with semicolons, so what I have now is a file where some lines have 10 fields, some lines have 11 fields, and sometimes the field separator is a space, and in other cases it's a semicolon, and some fields have legitimate embedded spaces (Description and Mechanic).</p>
  
  <p>I'm trying to find a way with awk to:</p>
  
  <p>1) Extract each field and be able to print it out with a uniform OFS (semicolon is preferred)</p>
  
  <p>2) If the Mechanic field is missing, insert it and print N/A or -- for the Mechanic</p>
  
  <p>I can deal with column headings and stuff myself, I just can't crack the code for how to deal with the FS problem and variable number of columns in this file. I can grep out specific information that I need, but would be thrilled to get it into a form where I can import it into a spreadsheet or DB.</p>