Split?

39 views
Skip to first unread message

Tom

unread,
Jun 4, 2019, 1:22:40 PM6/4/19
to
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. Any idea what I'm doing wrong? Why is it not spliting the line variable into an array?

Thanks in advance for any help!
Tom

Chad Rexin

unread,
Aug 8, 2019, 6:51:03 PM8/8/19
to
I believe this has to do with how the Write-host is working. You should get what you are expecting if you tell it to process the variable and then include that in the string. This would be done with the Write-Host line becoming like this, noting that the $() syntax is what tells it to process that variable first before doing the write-host/string processing. There's probably a better way of explaining this, but I tested this with your example and it will return "E1: Y21" when you use the Write-Host line below.

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

Chad
Reply all
Reply to author
Forward
0 new messages