Windows Powershell排序和分組管道結果_PowerShell

來源:互聯網
上載者:User

使用Sort-Object和Group-Object可以對管道結果進行分組。
其實每條命令執行後的結果已經排過序了。例如通過ls 查看檔案清單,預設會根據Name屬性進行排序,但是你可以通過指定屬性進行排序例如:

PS C:Powershell> ls | Sort-Object LengthMode     LastWriteTime Length Name----     ------------- ------ -----a--- 2011/11/28   15:30   63 ping.bat-a--- 2011/12/2   18:47  140 test.ps1-a--- 2011/11/28   16:42  170 test.vbs-a--- 2011/11/28   11:12  186 LogoTestConfig.xml-a--- 2011/11/23   17:37  242 test.txt-a--- 2011/11/25   11:20  556 employee.xml

這樣預設會根據length進行升序排序,如果要降序排列,可是使用Descending選項。

PS C:Powershell> ls | Sort-Object Length -DescendingMode     LastWriteTime Length Name----     ------------- ------ -----a--- 2011/11/24   17:44 735892 Powershell_Cmdlets.html-a--- 2011/11/24   18:30 67580 a.html-a--- 2011/11/24   20:04 26384 a.txt-a--- 2011/11/29   19:23 21466 function.ps1-a--- 2011/11/24   20:26 12060 alias-a--- 2011/11/24   17:37  7420 name.html

給對象和雜湊表進行排序

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

PS C:Powershell> Dir | Sort-Object Length, Name -descending, -ascendingSort-Object : 找不到接受實際參數“System.Object[]”的位置形式參數。所在位置 行:1 字元: 18+ Dir | Sort-Object <<<< Length, Name -descending, -ascending  + CategoryInfo     : InvalidArgument: (:) [Sort-Object], ParameterBin  dingException  + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell  .Commands.SortObjectCommand

但是上面的方法行不通,可是這樣操作:

PS C:Powershell> Dir | Sort-Object @{expression="Length";Descending=$true},@{expression="Name";Ascending=$true}  目錄: C:PowershellMode     LastWriteTime Length Name----     ------------- ------ -----a--- 2011/11/24   17:44 735892 Powershell_Cmdlets.html-a--- 2011/11/24   18:30 67580 a.html-a--- 2011/11/24   20:04 26384 a.txt-a--- 2011/11/29   19:23 21466 function.ps1-a--- 2011/11/24   20:26 12060 alias-a--- 2011/11/24   17:37  7420 name.html-a--- 2011/12/14   11:22  3460 ls.html-a--- 2011/11/30   16:04  2556 psdrive.html-a--- 2011/11/25   11:20  556 employee.xml-a--- 2011/11/23   17:37  242 test.txt-a--- 2011/11/28   11:12  186 LogoTestConfig.xml-a--- 2011/11/28   16:42  170 test.vbs-a--- 2011/12/2   18:47  140 test.ps1

對資料進行分組

如果想查看當前關閉和開啟的所有服務,並且通過狀態進行分組。可是使用:

PS C:Powershell> Get-Service | Group-Object StatusCount Name  Group----- ----  -----  87 Running {System.ServiceProcess.ServiceController, System.ServiceProcess.S       erviceController, System.ServiceProcess.ServiceController, System       .ServiceProcess.ServiceController...}  88 Stopped {System.ServiceProcess.ServiceController, System.ServiceProcess.S       erviceController, System.ServiceProcess.ServiceController, System       .ServiceProcess.ServiceController...}

再舉一例,把目前的目錄的檔案以副檔名進行分組

PS C:Powershell> ls | Group-Object ExtensionCount Name Group----- ---- -----  2    {ABC, alias}  5 .html {a.html, ls.html, name.html, Powershell_Cmdlets.html...}  2 .txt {a.txt, test.txt}  2 .xml {employee.xml, LogoTestConfig.xml}  2 .ps1 {function.ps1, test.ps1}  1 .bat {ping.bat}  1 .vbs {test.vbs}

使用運算式分組

如果要查看目前的目錄的檔案,根據檔案的大小是否大於1kb分組。

PS C:Powershell> ls | Group-Object {$_.Length -gt 1kb}Count Name           Group----- ----           -----  7 False           {ABC, employee.xml, LogoTestConfig.xml, ping...  8 True           {a.html, a.txt, alias, function.ps1...}

如果按照檔案名稱的首字母分組

PS C:Powershell> ls | Group-Object {$_.name.SubString(0,1).toUpper()}Count Name Group----- ---- -----  3 A  {a.html, a.txt, alias}  1 E  {employee.xml}  1 F  {function.ps1}  2 L  {LogoTestConfig.xml, ls.html}  1 N  {name.html}  3 P  {ping.bat, Powershell_Cmdlets.html, psdrive.html}  3 T  {test.ps1, test.txt, test.vbs}

根據當前應用程式的發行者分組

PS C:Powershell> Get-Process | Group-Object Company -NoElementCount Name----- ----  2 Adobe Systems Incorpor...  52  2 微軟  22 Microsoft Corporation  1 Adobe Systems, Inc.  1 Microsoft (R) Corporation  1  1 NVIDIA Corporation

使用格式化命令分組

Group-Object並不是唯一可以完成分組功能的命令,事實上格式化命令例如Format-Object支援一個GroupBy的參數,也可以完成分組。

PS C:Powershell> Dir | Sort-Object Extension, Name | Format-Table -groupBy Extension  目錄: C:PowershellMode        LastWriteTime   Length Name----        -------------   ------ -----a---    2011/11/24   20:26   12060 alias  目錄: C:PowershellMode        LastWriteTime   Length Name----        -------------   ------ -----a---    2011/11/28   15:30     63 ping.bat  目錄: C:PowershellMode        LastWriteTime   Length Name----        -------------   ------ -----a---    2011/11/24   18:30   67580 a.html-a---    2011/12/14   11:22    3460 ls.html-a---    2011/11/24   17:37    7420 name.html-a---    2011/11/24   17:44   735892 Powershell_Cmdlets.html-a---    2011/11/30   16:04    2556 psdrive.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.