1

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 派ですが、これは 1 つの awk ステートメントで簡単にできます。1 つのコマンドでこれを実行する方法を知っている場合は、それを行うか、以下の $d 構文の問題点を教えてください。

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

2 に答える 2

0

あなたのライン

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

$a の代わりに $b 変数を参照する必要があります

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

次に、 $d の出力が次のようになることに気付くでしょう。

hostOSVersion
8.0.2
7-Mode

そして、次のようなステートメント$d[1..2] -join ' 'が生成されます8.0.2 7-Mode

于 2012-07-30T11:34:45.347 に答える
0

正確なパターンをいじる必要があるかもしれませんが、1 つの方法は正規表現を使用することです。

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

本当に -replace で一列に並べたい場合:

$($($b -replace $a, '') -replace '\s{2}', '').trim()
于 2012-05-18T15:54:07.180 に答える