Powershell 按特定字符对字符串数组进行排序

问题描述 投票:0回答:3

我正在使用一个 powershell 脚本来自动执行一些长时间运行的部署任务。

我正在使用

$scripts = Get-ChildItem -r -Path server-deploy | select -expand fullname

得到一个看起来像这样的数组

D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-10.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-20.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-30.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-40.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-50.ps1

我在那里留下了一些“漏洞”,以便在需求发生变化时添加额外的脚本。脚本将始终被命名为“install-someword-int.ps1”,并且顺序由 int 决定。

我知道 Get-ChildItem 已按正确的顺序对它们进行排序,但我希望能够创建一个始终可以对它们进行排序的函数,因此如果我需要添加一个脚本,例如 install-builder-15.ps1 ,我知道它将在 10 点之后和 20 点之前运行。作为一名优秀的保管人,我本质上并不相信 Get-ChildItem 总是按正确的顺序返回它们。

我正在寻找对 $scripts 进行排序的正确方法,或使用某种 for 循环来迭代数组,但始终以正确的顺序。

编辑:@boxdog 使用文件:

install-builder-10.ps1                                                                                                                 
install-builder-20.ps1                                                                                                                 
install-builder-30.ps1                                                                                                                 
install-builder-4.ps1                                                                                                                  
install-builder-5.ps1 

如果我运行

Sort-Object @{e={$_ -match ".*install-.*-(?<number>\d+).ps1"; $matches.number}}
我会看到它们的顺序相同。我会期待

install-builder-4.ps1                                                                                                                  
install-builder-5.ps1 
install-builder-10.ps1                                                                                                                 
install-builder-20.ps1                                                                                                                 
install-builder-30.ps1     
powershell sorting
3个回答
4
投票

这是“按计算值排序”想法的另一种变体。这会在对它们进行排序之前将结尾数字转换为

[int]
。 [咧嘴笑]

$FakeFileList = @(
    [System.IO.FileInfo]'install-builder-10.ps1'
    [System.IO.FileInfo]'install-builder-20.ps1'
    [System.IO.FileInfo]'install-builder-30.ps1'
    [System.IO.FileInfo]'install-builder-4.ps1'
    [System.IO.FileInfo]'install-builder-5.ps1'
    )

$FakeFileList |
    Sort-Object {[int]$_.BaseName.Split('-')[-1]}

输出...

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
darhsl       1600-12-31   6:00 PM                install-builder-4.ps1
darhsl       1600-12-31   6:00 PM                install-builder-5.ps1
darhsl       1600-12-31   6:00 PM                install-builder-10.ps1
darhsl       1600-12-31   6:00 PM                install-builder-20.ps1
darhsl       1600-12-31   6:00 PM                install-builder-30.ps1

3
投票

如果您感兴趣的是按字符串末尾的 int 排序,那么您可以使用

Sort-Object
对它们进行排序,如下所示:

$paths = "D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-30.ps1",
         "D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-sortofbuilder-20.ps1",
         "D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-10.ps1",
         "D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-20.ps1",
         "D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-50.ps1",
         "D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-notbuilder-10.ps1",
         "D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-15.ps1"

$paths | Sort-Object @{e={$_ -match ".*install-.*-(?<number>\d+).ps1" | Out-Null; [int]$matches.number}}

这给出了这个输出:

D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-10.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-notbuilder-10.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-15.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-sortofbuilder-20.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-20.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-30.ps1
D:\BitBucket\CI\serverdeploy\builder\server-deploy\install-builder-50.ps1

0
投票

您可以对字符串数组进行排序 第一个整数匹配像这样。

$a =("__133____2_.ps1",
     "__13____12_.ps1",
     "__3____122_.ps1")

"`n=============SORTED AS NUMBERS by 1st occuring number"
$a|Sort-Object @{e={$_ -match ".*?(\d+).*?(\d+).*" Out-Null;[int]$matches[1]} }               

enter image description here


对于第二场/最后一场比赛替换:

$matches[1] 与 ( $matches[2] OR $matches.2 )

© www.soinside.com 2019 - 2024. All rights reserved.