title: Powershell - convert log file to CSV
body: <p>I have log files that look like this...</p>
  
  <pre><code>2009-12-18T08:25:22.983Z     1         174 dns:0-apr-credit-cards-uk.pedez.co.uk P http://0-apr-credit-cards-uk.pedez.co.uk/ text/dns #170 20091218082522021+89 sha1:AIDBQOKOYI7OPLVSWEBTIAFVV7SRMLMF - -
  2009-12-18T08:25:22.984Z     1           5 dns:0-60racing.co.uk P http://0-60racing.co.uk/ text/dns #116 20091218082522037+52 sha1:WMII7OOKYQ42G6XPITMHJSMLQFLGCGMG - -
  2009-12-18T08:25:23.066Z     1          79 dns:0-addiction.metapress.com.wam.leeds.ac.uk P http://0-addiction.metapress.com.wam.leeds.ac.uk/ text/dns #042 20091218082522076+20 sha1:NSUQN6TBIECAP5VG6TZJ5AVY34ANIC7R - -
  ...plus millions of other records
  </code></pre>
  
  <p>I need to convert these into csv files...</p>
  
  <pre><code>"2009-12-18T08:25:22.983Z","1","174","dns:0-apr-credit-cards-uk.pedez.co.uk","P","http://0-apr-credit-cards-uk.pedez.co.uk/","text/dns","#170","20091218082522021+89","sha1:AIDBQOKOYI7OPLVSWEBTIAFVV7SRMLMF","-","-"
  "2009-12-18T08:25:22.984Z","1","5","dns:0-60racing.co.uk","P","http://0-60racing.co.uk/","text/dns","#116","20091218082522037+52","sha1:WMII7OOKYQ42G6XPITMHJSMLQFLGCGMG","-","-"
  "2009-12-18T08:25:23.066Z","1","79","dns:0-addiction.metapress.com.wam.leeds.ac.uk","P","http://0-addiction.metapress.com.wam.leeds.ac.uk/","text/dns","#042","20091218082522076+20","sha1:NSUQN6TBIECAP5VG6TZJ5AVY34ANIC7R","-","-"
  </code></pre>
  
  <p>The field delimiter can be either a single or multiple space characters, with both fixed width and variable width fields. This tends to confuse most CSV parsers that I find.</p>
  
  <p>Ultimately I want to bcp these files into SQL Server but you can only specify a single character as a field delimiter (i.e. ' ') and this breaks the fixed length fields.</p>
  
  <p>So far - I'm using PowerShell</p>
  
  <pre><code>gc -ReadCount 10 -TotalCount 200 .\crawl_sample.log | foreach { ([regex]'([\S]*)\s+').matches($_) } | foreach {$_.Groups[1].Value}
  </code></pre>
  
  <p>and this returns a stream of the fields:</p>
  
  <pre><code>2009-12-18T08:25:22.983Z
  1
  74
  dns:0-apr-credit-cards-uk.pedez.co.uk
  P
  http://0-apr-credit-cards-uk.pedez.co.uk/
  text/dns
  #170
  20091218082522021+89
  sha1:AIDBQOKOYI7OPLVSWEBTIAFVV7SRMLMF
  -
  -
  2009-12-18T08:25:22.984Z
  1
  55
  dns:0-60racing.co.uk
  P
  http://0-60racing.co.uk/
  text/dns
  #116
  20091218082522037+52
  sha1:WMII7OOKYQ42G6XPITMHJSMLQFLGCGMG
  -
  </code></pre>
  
  <p>but how do I convert that output into the CSV format?  </p>