Powershell数组排序并分离成两个不同的数组

u5i3ibmn  于 2023-04-06  发布在  Shell
关注(0)|答案(6)|浏览(167)

我有这个数组:

[

"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]

然后把它整理出来,做两个像这样的独立数组

[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo"
]

[
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]

希望你的帮助!谢谢:)

epfja78i

epfja78i1#

简单数组过滤:

$arr="org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"

$arr2 = $arr.where{$PSItem -match 'joemarmoto'}
$arr3 = $arr.where{$PSItem -notmatch 'joemarmoto'}
gcuhipw9

gcuhipw92#

一个稍微不同的方法是使用Group-Object cmdlet在计算属性上分组.你没有指定要排序的确切部分-虚拟电子邮件或它后面的名称-所以我使用了虚拟电子邮件后面的部分.
这个结果给你一个数组的集合,有一个名字[计算的属性]和一个组[匹配那个计算的属性的项目]。你可以单独寻址它们或者使用它们来创建新的数组。

$AllNames = @(
    'org-dummyemail-123-AccountName',
    'org-dummyemail-123-Email',
    'org-dummyemail-123-PAT',
    'org-dummyemail-123-PATName',
    'org-dummyemail-123-PATValidTo'
    'org-dummyemail1-joemarmoto-AccountName',
    'org-dummyemail1-joemarmoto-Email',
    'org-dummyemail1-joemarmoto-PAT',
    'org-dummyemail1-joemarmoto-PATName',
    'org-dummyemail1-joemarmoto-PATValidTo'
    )

# 3rd item index = 2
$PartToGroupOn = 2

$GroupedAllNames = $AllNames |
    Group-Object -Property {$_.Split('-')[$PartToGroupOn]}

$GroupedAllNames[1].Group

输出

org-dummyemail-123-AccountName
org-dummyemail-123-Email
org-dummyemail-123-PAT
org-dummyemail-123-PATName
org-dummyemail-123-PATValidTo
hmmo2u0o

hmmo2u0o3#

将您的输入视为Json对象(来自here字符串)脚本

  • 使用Select-Object和从前3个-/破折号分隔的部分构建key

计算属性。

  • 按此键分组
  • 从组值重建Json对象。
$Json = @"
[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]
"@ | ConvertFrom-JSon

$Json | Select-Object @{n='key';E={($_ -split '-')[0..2] -join '-'}},
                      @{n='value';e={$_}} |
    Group-Object key | ForEach-Object{
        $_.Group.Value | ConvertTo-Json
    }

具有以下输出:

[
    "org-dummyemail-123-AccountName",
    "org-dummyemail-123-Email",
    "org-dummyemail-123-PAT",
    "org-dummyemail-123-PATName",
    "org-dummyemail-123-PATValidTo"
]
[
    "org-dummyemail1-joemarmoto-AccountName",
    "org-dummyemail1-joemarmoto-Email",
    "org-dummyemail1-joemarmoto-PAT",
    "org-dummyemail1-joemarmoto-PATName",
    "org-dummyemail1-joemarmoto-PATValidTo"
]
jdg4fx2g

jdg4fx2g4#

下面是一个使用regex来确定输入字符串的哪一部分分组的版本:

clear-host

'REGEX 1; org-dummyemail-123 vs org-dummyemail1-joemarmoto'

$groupedByAllButLast = @(
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
) | Group-Object -Property @{E={$_ -replace '([^-]+-[^-]+-[^-]+)-.*', '$1'}} 

for ([int]$i = 0; $i -lt $groupedByAllButLast.Count; $i++)
{
    Write-Verbose $groupedByAllButLast[$i].Name -Verbose
    $groupedByAllButLast[$i].Group
}

'REGEX 2; 123 vs joemarmoto'
$groupedByTheNameOrEmailBit = @(
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
) | Group-Object -Property @{E={$_ -replace '[^-]+-[^-]+-([^-]+)-.*', '$1'}} #all we've changed is the location of the opening bracket in this line 

for ([int]$i = 0; $i -lt $groupedByTheNameOrEmailBit.Count; $i++)
{
    Write-Verbose $groupedByTheNameOrEmailBit[$i].Name -Verbose
    $groupedByTheNameOrEmailBit[$i].Group
}

注意:这与Lee's的方法基本相同;只不过我们没有使用split来获取单个段,而是使用regex来捕获字符串中与我们的键相关的所有部分...关于这个有用的函数,请参阅MS Docs以获得更多信息。

dzhpxtsq

dzhpxtsq5#

使用$PSitem变量

$arr="org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"

$arr2 = $arr | Where-Object {$PSItem -match 'joemarmoto'}
$arr3 = $arr | Where-Object {$PSItem -match '123'}

看看这里:https://blogs.msdn.microsoft.com/mvpawardprogram/2013/04/15/working-with-the-new-psitem-automatic-variable-in-windows-powershell-3-0/
从该网站:获取有关_Automatic_Variables的帮助-ShowWindow
在“查找”框中键入“psitem”以突出显示包含此单词的所有文本:
正如我们所看到的,$PSitem和$_是一样的。引入$PSitem变量的目的是让包含“管道中的当前对象”的代码更容易阅读和理解。
答案与Gert Jan Kraaijeveld几乎相同,但解释了$PSitem的作用以及信息的来源。

j2datikz

j2datikz6#

同样的想法,但有一个哈希表:

$json = @'
[
"org-dummyemail-123-AccountName",
"org-dummyemail-123-Email",
"org-dummyemail-123-PAT",
"org-dummyemail-123-PATName",
"org-dummyemail-123-PATValidTo",
"org-dummyemail1-joemarmoto-AccountName",
"org-dummyemail1-joemarmoto-Email",
"org-dummyemail1-joemarmoto-PAT",
"org-dummyemail1-joemarmoto-PATName",
"org-dummyemail1-joemarmoto-PATValidTo"
]
'@

$a = $json | ConvertFrom-Json
$b = @{}

$a | ForEach-Object {
    $b[$_.Split('-')[0..2] -join '-'] += @($_)
}

$b | ConvertTo-Json

相关问题