Trouble Splitting String? RRS feed

  • Question

  • Greetings, 

    I have a file with: 

    Y21 M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved 
    Y22 M3_TRSTB JTAG_TRSTB/M3_TRSTB Reserved 

    I have the following code to read  the file then try to process 
    each element on the line: 

    $a = Get-Content -Path .\pin.txt 

    foreach ($line in $a) { 
            #$element = $line.Split(" ") 
            $element = $line -split "\s+" 
            Write-Host "E1: $element[0]" 

    However the .Split or -split doesn't work? I get: 

    E1: Y21 M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved[0] 
    E1: Y22 M3_TRSTB JTAG_TRSTB/M3_TRSTB Reserved[0] 

    I've tied to convert to a string or  pipe to Out-String on the reading, but 
    that didn't work. I also checked to see if it thinks there's a space in the line with -contain, Powershell doesn't see this as a string? Any idea what I'm doing wrong? Why is it not spliting the line variable into an array? 

    Thanks in advance for any help! 

    Wednesday, June 5, 2019 11:10 AM

All replies

  • Simpler like this:

    PS D:\scripts> $e1,$e2,$e3,$e4 = 'Y21 M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved' -split '\s'
    PS D:\scripts> $e1,$e2,$e3,$e4
    PS D:\scripts>


    Wednesday, June 5, 2019 11:20 AM
  • Hi,

    Thanks, that good, but I need to push into an array, since in the future there could be 1..N fields and I'll need to test the $e size to figure out what to do with each line... So I'm trying to split into an array:

    But stupid me, the problem is in the Write-Host:

    Write-Host "E1: $element[0]" 

    That should be:

    Write-Host "E1: ", $element[0], " E2: ", $element[1]

    The "$element[0]" can't be inside the quotes (").... a bit of a surprise to be, this behaves more like Python than Perl....

    Thanks Again!


    Wednesday, June 5, 2019 1:01 PM
  • So use an array.

    $arr = 'Y21 M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved' -split '\s'

    That is all it takes.


    Wednesday, June 5, 2019 1:14 PM
  • You don't need the Write-Host. Try:

    "E1=" + $element[0]

    or, if you want to stick with Write-Host:

    Write-Host "E1=" $element[0]

    But, of course, jrv's proposal seems to me better.


    Wednesday, June 5, 2019 2:01 PM
  • You cannot use the "Split" method with a regular expression.  Use the "-split" operator.


    Wednesday, June 5, 2019 2:12 PM
  • I'd make a minor change the regex:

        $arr = 'Y21 M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved' -split '\s+'

    . . . just in case there's more than one whitespace character separating the non-whitespace characters

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, June 5, 2019 2:18 PM
  • It does? Doesn't that just return two substrings????

    How about this one (it splits on whitespace)? It's even easier!

    $arr = -split "Y21       M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved"

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, June 5, 2019 2:43 PM
  • Sorry Rich.  You are correct.


    • Edited by jrv Wednesday, June 5, 2019 3:13 PM
    Wednesday, June 5, 2019 3:13 PM
  • You can use $element[0] inside quotes like this:

       "This is element zero: $($element[0])"

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, June 5, 2019 3:21 PM
  • How about this?

    Get-Content -Path .\pin.txt |
        foreach { 
            $arr = -split $_

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, June 5, 2019 3:22 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Friday, July 5, 2019 2:06 AM