我有一个包含此内容的文本文件:
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
的{{1}},worker02
,worker03
,worker04
(不带前导#)作为数组。我如何实现这一目标?
答案 0 :(得分: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
答案 1 :(得分: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
通过使用哈希表,我们可以非常轻松地消除重复的条目。
答案 2 :(得分:1)
你可能需要稍微调整一下才能得到你想要的东西,但这应该让你朝着正确的方向前进:
$workers = (Get-Content $strPath"\worker\conf\segments.conf" | ?{$_ -like 'Worker_Segments*'}).Split('"')[1].Split(' ')