I have a text file with this content:
我有一个包含以下内容的文本文件:
SEGMENTS="worker01 worker02 worker03 worker04"
WORKER_SEGMENTS="worker01 worker02 worker03 worker04"
#SEGMENTS="worker01"
#WORKER_SEGMENTS="worker01"
I read this file from another powershell script and I want to to create an array of the WORKER_SEGMENTS
values. I have gotten so far:
我从另一个PowerShell脚本中读取此文件,我想创建一个WORKER_SEGMENTS值的数组。我到目前为止:
$workers = Get-Content $strPath"\worker\conf\segments.conf" | Where-Object {$_ -like "*WORKER_SEGMENTS*"}
Write-Host $workers
This yields:
PS Q:\mles\etl-i_test\rc> .\etl.ps1
WORKER_SEGMENTS="worker01 worker02 worker03 worker04" #WORKER_SEGMENTS="worker01"
I only need the worker01
, worker02
, worker03
, worker04
from WORKER_SEGMENTS
(without the leading #) as an array. How do I achieve this?
我只需要WORKER_SEGMENTS中的worker01,worker02,worker03,worker04(没有前导#)作为数组。我该如何实现这一目标?
3 个解决方案
#1
2
You can try this:
你可以试试这个:
Get-Content .\t.txt |
? { $_ -like "WORKER_SEGMENTS*" } |
% { $_ -replace '.*?"([^"]*)".*', '$1' -split " " }
worker01
worker02
worker03
worker04
Or
Get-Content .\t.txt |
? { $_ -like "WORKER_SEGMENTS*" } |
% { ($_ -split '"')[1] -split ' ' }
worker01
worker02
worker03
worker04
#2
2
Try this:
Get-Content $strPath"\worker\conf\segments.conf" |
Select-String 'WORKER_SEGMENTS\s*=\s*"([^"]*)"' |
Foreach {$_.Matches.Groups[1].Value -split ' '} |
Foreach {$ht=@{}}{$ht.$_=$null}
$ht
By using a hashtable we can eliminate duplicate entries fairly easily.
通过使用哈希表,我们可以非常轻松地消除重复的条目。
#3
0
you may have to tweak it a bit to get exactly what you want, but this should get you headed in the right direction:
你可能需要稍微调整一下才能得到你想要的东西,但这应该让你朝着正确的方向前进:
$workers = (Get-Content $strPath"\worker\conf\segments.conf" | ?{$_ -like 'Worker_Segments*'}).Split('"')[1].Split(' ')
#1
2
You can try this:
你可以试试这个:
Get-Content .\t.txt |
? { $_ -like "WORKER_SEGMENTS*" } |
% { $_ -replace '.*?"([^"]*)".*', '$1' -split " " }
worker01
worker02
worker03
worker04
Or
Get-Content .\t.txt |
? { $_ -like "WORKER_SEGMENTS*" } |
% { ($_ -split '"')[1] -split ' ' }
worker01
worker02
worker03
worker04
#2
2
Try this:
Get-Content $strPath"\worker\conf\segments.conf" |
Select-String 'WORKER_SEGMENTS\s*=\s*"([^"]*)"' |
Foreach {$_.Matches.Groups[1].Value -split ' '} |
Foreach {$ht=@{}}{$ht.$_=$null}
$ht
By using a hashtable we can eliminate duplicate entries fairly easily.
通过使用哈希表,我们可以非常轻松地消除重复的条目。
#3
0
you may have to tweak it a bit to get exactly what you want, but this should get you headed in the right direction:
你可能需要稍微调整一下才能得到你想要的东西,但这应该让你朝着正确的方向前进:
$workers = (Get-Content $strPath"\worker\conf\segments.conf" | ?{$_ -like 'Worker_Segments*'}).Split('"')[1].Split(' ')