2012-05-18 166 views
1

我在解析PowerShell中的一些字符串数据时遇到问题,需要一点帮助。基本上我有一个不输出对象的应用程序命令,而是字符串数据。Powershell - 搜索字符串,删除多余的空白,打印第二个字段

a = is the item I'm searching for 
b = is the actual ouput from the command 
c = replaces all the excess whitespace with a single space 
d = is supposed to take $c "hostOSVersion 8.0.2 7-Mode" and just print "8.0.2 7-Mode" 

但是,$ d不起作用,它只是打印与$ c相同的值。我是一个UNIX家伙,在一个awk语句中这很容易。如果你知道如何在一个很好的命令中做到这一点,或者告诉我下面的$ d语法有什么问题。

$a = "hostOSVersion" 
$b = "hostOSVersion       8.0.2 7-Mode" 
$c = ($a -replace "\s+", " ").Split(" ") 
$d = ($y -replace "$a ", "") 

回答

0

那么你可能有确切的模式futz左右,但一个方法是使用正则表达式:

$b = "hostOSVersion       8.0.2 7-Mode" 
$b -match '(\d.*)' 
$c = $matches[1] 

如果你真的想与-replace到ONELINE它:

$($($b -replace $a, '') -replace '\s{2}', '').trim() 
+0

谢谢主席先生,那第二个单线程做了诀窍。 – user1403741

0

您的线路

$c = ($a -replace "\s+", " ").Split(" ") 

s HOULD参考$ b变量,而不是$一个

$c = ($b -replace "\s+", " ").Split(" ") 

然后,你会注意到$ d的输出成为

hostOSVersion 
8.0.2 
7-Mode 

和像$d[1..2] -join ' '语句会产生8.0.2 7-Mode

相关问题