分割Select-String

5
给定这个变量
$foo = help | Select-String powershell

尝试分割将失败。
PS > $foo.split()
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo]
does not contain a method named 'split'.
At line:1 char:1
+ $foo.split()
+ ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

我该如何拆分这个变量?
2个回答

14
匹配行的字符串值位于MatchInfo对象的Line属性中。
$foo.Line.split()

1

Split是一个字符串方法,因此必须在一个字符串上调用。

[string] $foo = help | Select-String powershell

或者

$foo = help | Select-String powershell | Out-String

3
虽然这个方法可行,但并没有完全体现 PowerShell 的本质,即它输出的是对象而不仅仅是文本。 :-) - Keith Hill

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接