3

我有一个包含以下内容的文本文件:

SEGMENTS="worker01 worker02 worker03 worker04"
WORKER_SEGMENTS="worker01 worker02 worker03 worker04"
#SEGMENTS="worker01"
#WORKER_SEGMENTS="worker01"

我从另一个 powershell 脚本中读取了这个文件,我想创建一个WORKER_SEGMENTS值数组。我到目前为止:

$workers = Get-Content $strPath"\worker\conf\segments.conf" | Where-Object {$_ -like "*WORKER_SEGMENTS*"}
Write-Host $workers

这产生:

PS Q:\mles\etl-i_test\rc> .\etl.ps1
WORKER_SEGMENTS="worker01 worker02 worker03 worker04" #WORKER_SEGMENTS="worker01"

我只需要worker01, worker02, worker03, worker04from WORKER_SEGMENTS(没有前导 #)作为数组。我如何实现这一目标?

4

3 回答 3

3

你可以试试这个:

Get-Content .\t.txt | 
? { $_ -like "WORKER_SEGMENTS*" } | 
% { $_ -replace '.*?"([^"]*)".*', '$1' -split " " }

worker01
worker02
worker03
worker04

或者

Get-Content .\t.txt | 
? { $_ -like "WORKER_SEGMENTS*" } | 
% { ($_ -split '"')[1] -split ' ' }

worker01
worker02
worker03
worker04
于 2013-05-03T15:49:04.610 回答
2

试试这个:

Get-Content $strPath"\worker\conf\segments.conf" | 
    Select-String 'WORKER_SEGMENTS\s*=\s*"([^"]*)"' | 
    Foreach {$_.Matches.Groups[1].Value -split ' '} | 
    Foreach {$ht=@{}}{$ht.$_=$null}
$ht

通过使用哈希表,我们可以相当容易地消除重复条目。

于 2013-05-03T15:51:08.217 回答
1

你可能需要稍微调整一下才能得到你想要的,但这应该会让你朝着正确的方向前进:

$workers = (Get-Content $strPath"\worker\conf\segments.conf" | ?{$_ -like 'Worker_Segments*'}).Split('"')[1].Split(' ')
于 2013-05-03T15:46:43.697 回答