title: PowerShell: Pattern Matching Text File Contents to Insert into .CSV
body: <p>I have been struggling to successfully break apart contents of a text file and insert them into a .csv with the following rules:</p>
  <ol>
  <li>The line containing '&gt;' should be inserted into .csv column 1</li>
  <li>The lines containing all caps should be inserted into .csv column 2 and each block of capital letters should be joined (have its `r or `n removed)</li>
  <li>'&gt;' and '*' should be removed where present</li>
  </ol>
  <p>Separately, I can get column 1 to work fairly well using:</p>
  <pre><code>$file = (Get-Content 'samplefile.txt')
  
  $data = foreach ($line in $file) {
      if ($line -match '^&gt;') {
              [pscustomobject]@{
              'Part1' = (Select-String '^&gt;' -InputObject $line) -replace '&gt;', ''
              }
  }
  }
  $data | Out-File 'newfile.csv'
  </code></pre>
  <p>and limited success using similar for column 2 (I can't seem to get <code>-join</code> to work with `r or `n):</p>
  <pre><code>$file = (Get-Content 'samplefile.txt')
  
  $data = foreach ($line in $file) {
      if ($line -match '^[A-Z].*') {
              [pscustomobject]@{
              'Part2' = (Select-String '^[A-Z].*' -InputObject $line) -replace '*', ''
              }
  }
  }
  $data | Out-File 'newfile.csv'
  </code></pre>
  <p>But it escapes me how to get both to work in the same code block to iterate over each section delimited by '&gt;' and/or '*'.</p>
  <p>Below is a sample of the data for reference.</p>
  <pre>
  >9392290|2983921
  FYUOIQWEFYUOIAGSNJJJHKEWAHJKTHJEWUYIYGUIOIOIUYAFUIOWUEYOUYIA
  GDFOUYUIOAGHIHUAGSD
  >lsm.VI.superconfig_5640.1|lsm.model.superconfig_5640.1
  FDASJKLHJKLGAHJKDFGHJKAGJKHUIGAHIULGRUOUHWWUGUIOHZIOJSHIJMAW
  DFSANJKLNJLWEQUIOGFDSOIYUBHPOGANUPPUNABNPUNUPAPNUNPUFSAPNUSS
  FSADUHHULGWAUNUNWEANNIOEAWNUNIIIINNBSDNJLKNJKLAERGJKLHHJLKGS
  DFSAQSAHUSDFAHOUHGROUGRWE*
  >jfi.ZJ.superconfig_99.31|jfi.model.superconfig_99.31
  ASDFUIOHPOASPNADPUNPNUSADFNUPPUOHZSABUHBAHPUDASPHAWHPOEWGHPI
  GWANUEGWUNPNPEANUPUNPEAWUPOGDFPOAGIJJIEOAWIOAGPIOJSGNJHIOWEA
  AUHNHIOEANPIASPNIOICBNIOASGIOEGWPIOWEPPPPSAJPOJKGPWEAIOJJPIO
  FAWEIOPHGAHNIOPGWEOPPOEAWSPIOOPUIGSUIOGUIOPWAGIEOUIWEAOGUIOP
  GEIOJHIOJPWEPJIOWGEIOPHGANIONIOGEWANIOEGWOPIHNNPIOEGWIJOWEAG
  GEPUIEWUIOSZBHJENWNBENUEBMIPEWVMIEMUIAZWIPNBWEPEWIOJJKEAWPIA
  GWEPHIOEWNPOEWANNNPIOGWREIJUOGUHIOSNJJJJJJJJKVMVIOIPEGIOEAUW
  EGWIOJNENIOPIOWINPEAWNPOI*
  </pre>