Code Efficiency – CSV Trim and Replace Operations

Welcome Forums General PowerShell Q&A Code Efficiency – CSV Trim and Replace Operations

Viewing 2 reply threads
  • Author
    Posts
    • #209799
      Participant
      Topics: 2
      Replies: 2
      Points: 9
      Rank: Member

      Hi

      My code below finds all CSVs recursively in a directory, loads located CSVs, converts the CSVs into TSVs, trims first and end double quotes and then removes all double quotes between a tab space.

      My Question:

      Is there a way to make the code more efficient? I can’t seem to find a way to remove the ForEach-Object loop. Please help. Thanks. (For some reason the accent symbol does not work to display the code below)

      t "" ,"t") } } } | Set-Content "$dest" } } [/crayon]
      • This topic was modified 10 months, 2 weeks ago by vertigo88858.
      • This topic was modified 10 months, 2 weeks ago by vertigo88858.
    • #209871
      Participant
      Topics: 5
      Replies: 260
      Points: 1,039
      Helping Hand
      Rank: Community Hero

      Which ForEach-Object loop are you trying to eliminate?  I count 3 of them in your code (line 12, 13, 15).  % is an alias for foreach-object.  Without samples of your csv, I think you might be better working with the raw data that multiple conversions (import-csv then  convertto-csv).  I’m not sure if my example below is more efficient, but you could use measure-command to see for sure.  Either way I find yours a little hard to read with nested loops, but that is just a preference.

    • #209895
      Participant
      Topics: 17
      Replies: 1956
      Points: 4,010
      Helping Hand
      Rank: Community Hero

      Loops are traditionally avoided if possible, but here are a couple of options. This is the CSV to mess with:

      This would parse using Import-CSV, so if there are “rogue” double qoutes in the content, this is parsing it as a CSV and then gluing it together with string builder. While this is using for loops, there is no Replace operations, it’s just parse and glue it together:

      n" -f ($row.PSObject.Properties.Name -join ';')) break } #Data Row foreach ($row in $csv) { [void]$myNewCSV.Append("{0} rn" -f ($row.PSObject.Properties.Value -join ';')) } $myNewCSV.ToString() #Set Content [/crayon]

      Output:

      Another approach is using Get-Content -Raw, which should bring the content in as raw content and not parse lines. This is available Powershell 4+.

      Output:

Viewing 2 reply threads
  • The topic ‘Code Efficiency – CSV Trim and Replace Operations’ is closed to new replies.