Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

Split?

已查看 39 次
跳至第一个未读帖子

Tom

未读,
2019年6月4日 13:22:402019/6/4
收件人
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

未读,
2019年8月8日 18:51:032019/8/8
收件人
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
0 个新帖子