Windows Powershell排序和分组管道结果示例

Windows Powershell排序和分组管道结果示例


使用Sort-Object和Group-Object可以对管道结果进行分组。
其实每条命令执行后的结果已经排过序了。例如通过ls 查看文件列表,默认会根据Name属性进行排序,但是你可以通过指定
属性进行排序:

例如:

PS C:\wang\VB_family\VBA\tool2> ls | Sort-Object Length

    Directory: C:\wang\VB_family\VBA\tool2

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2023/09/28    19:38             48 .gitignore
-a---          2023/09/28    19:38            315 package.json
-a---          2023/09/28    19:39            360 config.json
-a---          2023/09/28    20:42          10366 test.xlsx
-a---          2023/09/28    20:42          21547 test.xlsm
d----          2023/09/28    19:39                logs
d----          2023/09/28    19:38                ribbons
d----          2023/09/28    19:39                vba-files
d----          2023/09/28    19:38                xvba_modules
d----          2023/09/28    19:38                xvba_unit_test

PS C:\wang\VB_family\VBA\tool2> 

这样默认会根据length进行升序排序,如果要降序排列,可以使用Descending选项。

PS C:\wang\VB_family\VBA\tool2> ls | Sort-Object Length -Descending

    Directory: C:\wang\VB_family\VBA\tool2

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2023/09/28    20:42          21547 test.xlsm
-a---          2023/09/28    20:42          10366 test.xlsx
-a---          2023/09/28    19:39            360 config.json
-a---          2023/09/28    19:38            315 package.json
-a---          2023/09/28    19:38             48 .gitignore
d----          2023/09/28    19:39                logs
d----          2023/09/28    19:38                ribbons
d----          2023/09/28    19:39                vba-files
d----          2023/09/28    19:38                xvba_modules
d----          2023/09/28    19:38                xvba_unit_test

PS C:\wang\VB_family\VBA\tool2> 

给对象和哈希表进行排序

如果要完成主要关键字降序,次要关键字升序的排序,可能首先想到的是:

PS C:\wang\VB_family\VBA\tool2> Dir | Sort-Object Length, Name -descending, -ascending
ParserError: 
Line |
   1 |  Dir | Sort-Object Length, Name -descending, -ascending
     |                                            ~
     | Missing argument in parameter list.
PS C:\wang\VB_family\VBA\tool2>

但是上面的方法行不通,可以这样操作:

PS C:\wang\VB_family\VBA\tool2> Dir | Sort-Object @{expression="Length";Descending=$true},@{expression="Name";Ascending=$true}

    Directory: C:\wang\VB_family\VBA\tool2

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2023/09/28    20:42          21547 test.xlsm
-a---          2023/09/28    20:42          10366 test.xlsx
-a---          2023/09/28    19:39            360 config.json
-a---          2023/09/28    19:38            315 package.json
-a---          2023/09/28    19:38             48 .gitignore
d----          2023/09/28    19:39                logs
d----          2023/09/28    19:38                ribbons
d----          2023/09/28    19:39                vba-files
d----          2023/09/28    19:38                xvba_modules
d----          2023/09/28    19:38                xvba_unit_test

PS C:\wang\VB_family\VBA\tool2>

对数据进行分组

如果想查看当前关闭和开启的所有服务,并且通过状态进行分组。可是使用:

PS C:\wang\VB_family\VBA\tool2> Get-Service | Group-Object Status       
Get-Service: Service 'McpManagementService (McpManagementService)' cannot be queried due to the following error: 
Get-Service: Service 'NPSMSvc_6e472 (NPSMSvc_6e472)' cannot be queried due to the following error: 
Get-Service: Service 'WaaSMedicSvc (WaaSMedicSvc)' cannot be queried due to the following error: 

Count Name                      Group
----- ----                      -----
  192 Stopped                   {AarSvc_6e472, AJRouter, ALG, AntiCheatExpert Service…}
  122 Running                   {AlibabaProtect, Appinfo, AppXSvc, AudioEndpointBuilder…}

PS C:\wang\VB_family\VBA\tool2>

再举一例,把当前目录的文件以扩展名进行分组。

PS C:\wang\VB_family\VBA\tool2> ls | Group-Object Extension        

Count Name                      Group
----- ----                      -----
    5                           {C:\wang\VB_family\VBA\tool2\logs, C:\wang\VB_family\VBA\tool2\ribbons, C:\wang\VB_family\VBA\tool2\vba-files, C:\wang\VB_family\VBA\tool2\xvba_modules…}    
    1 .gitignore                {C:\wang\VB_family\VBA\tool2\.gitignore}
    2 .json                     {C:\wang\VB_family\VBA\tool2\config.json, C:\wang\VB_family\VBA\tool2\package.json}
    1 .xlsm                     {C:\wang\VB_family\VBA\tool2\test.xlsm}
    1 .xlsx                     {C:\wang\VB_family\VBA\tool2\test.xlsx}

PS C:\wang\VB_family\VBA\tool2>

使用表达式分组

如果要查看当前目录的文件,根据文件的大小是否大于1kb分组。

PS C:\wang\VB_family\VBA\tool2> ls | Group-Object {$_.Length -gt 1kb}

Count Name                      Group
----- ----                      -----
    8 False                     {C:\wang\VB_family\VBA\tool2\logs, C:\wang\VB_family\VBA\tool2\ribbons, C:\wang\VB_family\VBA\tool2\vba-files, C:\wang\VB_family\VBA\tool2\xvba_modules…}    
    2 True                      {C:\wang\VB_family\VBA\tool2\test.xlsm, C:\wang\VB_family\VBA\tool2\test.xlsx}

PS C:\wang\VB_family\VBA\tool2>

如果按照文件名的首字母分组

PS C:\wang\VB_family\VBA\tool2> ls | Group-Object {$_.name.SubString(0,1).toUpper()}

Count Name                      Group
----- ----                      -----
    1 .                         {C:\wang\VB_family\VBA\tool2\.gitignore}
    1 C                         {C:\wang\VB_family\VBA\tool2\config.json}
    1 L                         {C:\wang\VB_family\VBA\tool2\logs}
    1 P                         {C:\wang\VB_family\VBA\tool2\package.json}
    1 R                         {C:\wang\VB_family\VBA\tool2\ribbons}
    2 T                         {C:\wang\VB_family\VBA\tool2\test.xlsm, C:\wang\VB_family\VBA\tool2\test.xlsx}
    1 V                         {C:\wang\VB_family\VBA\tool2\vba-files}
    2 X                         {C:\wang\VB_family\VBA\tool2\xvba_modules, C:\wang\VB_family\VBA\tool2\xvba_unit_test}

PS C:\wang\VB_family\VBA\tool2>

根据当前应用程序的发布者分组

PS C:\wang\VB_family\VBA\tool2> Get-Process | Group-Object Company -NoElement

Count Name
----- ----
  135
    1
   21 Google LLC
    2 Logitech, Inc.
    1 McAfee LLC
    3 McAfee, LLC
   85 Microsoft Corporation
    2 Microsoft Corporation   …
    1 Oracle Corporation
    2 Project: Sakura-Editor
    2 Realtek Semiconductor
    4 Sogou.com
    4 Zoom Video Communication…

PS C:\wang\VB_family\VBA\tool2>

使用格式化命令分组

Group-Object并不是唯一可以完成分组功能的命令,事实上格式化命令例如Format-Object支持一个GroupBy的参数,也可以
完成分组。

PS C:\wang\VB_family\VBA\tool2> Dir | Sort-Object Extension, Name | Format-Table -groupBy Extension

    Directory: C:\wang\VB_family\VBA\tool2

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2023/09/28    19:39                logs
d----          2023/09/28    19:38                ribbons
d----          2023/09/28    19:39                vba-files
d----          2023/09/28    19:38                xvba_modules
d----          2023/09/28    19:38                xvba_unit_test

    Directory: C:\wang\VB_family\VBA\tool2

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2023/09/28    19:38             48 .gitignore

    Directory: C:\wang\VB_family\VBA\tool2

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2023/09/28    19:39            360 config.json
-a---          2023/09/28    19:38            315 package.json

    Directory: C:\wang\VB_family\VBA\tool2

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2023/09/28    20:42          21547 test.xlsm

    Directory: C:\wang\VB_family\VBA\tool2

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          2023/09/28    20:42          10366 test.xlsx

PS C:\wang\VB_family\VBA\tool2>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小泉映月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值