none
Split & Trim RRS feed

  • Question

  • I am looking to convert a comma delimited list into an array, and the list may or may not also have white space around the delimiters for readability, so I want to Trim post Split. Also, I am just dipping my toes into PowerShell, and trying to learn all the good habits, and my sense is that this is where the pipeline is powerful.

    So, I have the Split part

    $CSVData = @("One       ,   Two ,   Three    ") #extranious spaces just to make the point

    $Data = $CSVData -Split (",")


    However, I can't seem to pipe this to -Trim. Also, I have found a number of references to this idiom:
    %{$Data = $_.Split(",")} which I understand to be shorthand for ForEach, which seems to have potential, perhaps to handle the Trim after $Data is a populated array. But $_ is the pipeline content I think, not the elements of the array I think. And I am sure I don't understand the nuances of dot notation, and when that is preferable over other approaches and why. And it seems like there should be an elegant way to combine the Split and the Trim as a one liner.

    Any good resources that will get me headed in the right direction? 

    Thanks!
    Gordon

    Saturday, December 14, 2013 1:31 AM

Answers


  • Use foreach-object like that:

    $CSVData -split ',' | foreach {$_.Trim()}


    In PowerShell v3 and higher you can simplify it like that (the foreach part is implicitly done by PowerShell)

    $CSVData.Split(",").Trim()

    • Edited by Dirk_74 Saturday, December 14, 2013 1:57 AM
    • Proposed as answer by R Jason Morgan Tuesday, December 17, 2013 3:30 AM
    • Marked as answer by AnnaWYModerator Tuesday, December 24, 2013 12:28 PM
    Saturday, December 14, 2013 1:51 AM
  • PowerShell's split operator takes a regular expression, so you could just do it like this:

    $csvData -split '\s*,\s*'

    Though that wouldn't get rid of the spaces after the word "Three", since there was no delimiter at that point.

    Saturday, December 14, 2013 4:20 AM
  • $CSVData = "One       ,   Two ,   Three    "
    $CSVData.split(',') | %{$_.trim()}
    

    Monday, December 16, 2013 11:16 PM

All replies


  • Use foreach-object like that:

    $CSVData -split ',' | foreach {$_.Trim()}


    In PowerShell v3 and higher you can simplify it like that (the foreach part is implicitly done by PowerShell)

    $CSVData.Split(",").Trim()

    • Edited by Dirk_74 Saturday, December 14, 2013 1:57 AM
    • Proposed as answer by R Jason Morgan Tuesday, December 17, 2013 3:30 AM
    • Marked as answer by AnnaWYModerator Tuesday, December 24, 2013 12:28 PM
    Saturday, December 14, 2013 1:51 AM
  • PowerShell's split operator takes a regular expression, so you could just do it like this:

    $csvData -split '\s*,\s*'

    Though that wouldn't get rid of the spaces after the word "Three", since there was no delimiter at that point.

    Saturday, December 14, 2013 4:20 AM
  • Thanks guys! Slowly it begins to make sense. Now off to try to figure out passing parameters when launching from a shortcut.

    Gordon

    Saturday, December 14, 2013 7:30 AM
  • $CSVData = "One       ,   Two ,   Three    "
    $CSVData.split(',') | %{$_.trim()}
    

    Monday, December 16, 2013 11:16 PM