Windows Powershell排序和分組管道結(jié)果
使用Sort-Object和Group-Object可以對管道結(jié)果進(jìn)行分組。
其實(shí)每條命令執(zhí)行后的結(jié)果已經(jīng)排過序了。例如通過ls 查看文件列表,默認(rèn)會根據(jù)Name屬性進(jìn)行排序,但是你可以通過指定屬性進(jìn)行排序例如:
PS C:Powershell> ls | Sort-Object Length Mode 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
這樣默認(rèn)會根據(jù)length進(jìn)行升序排序,如果要降序排列,可是使用Descending選項(xiàng)。
PS C:Powershell> ls | Sort-Object Length -Descending Mode 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
給對象和哈希表進(jìn)行排序
如果要完成主要關(guān)鍵字降序,次要關(guān)鍵字升序的排序,可能首先想到的是:
PS C:Powershell> Dir | Sort-Object Length, Name -descending, -ascending Sort-Object : 找不到接受實(shí)際參數(shù)“System.Object[]”的位置形式參數(shù)。 所在位置 行: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},@{ex
pression="Name";Ascending=$true}
目錄: C:Powershell
Mode 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
對數(shù)據(jù)進(jìn)行分組
如果想查看當(dāng)前關(guān)閉和開啟的所有服務(wù),并且通過狀態(tài)進(jìn)行分組??墒鞘褂茫?/p>
PS C:Powershell> Get-Service | Group-Object Status
Count 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...}
再舉一例,把當(dāng)前目錄的文件以擴(kuò)展名進(jìn)行分組。
PS C:Powershell> ls | Group-Object Extension
Count 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}
使用表達(dá)式分組
如果要查看當(dāng)前目錄的文件,根據(jù)文件的大小是否大于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}
根據(jù)當(dāng)前應(yīng)用程序的發(fā)布者分組
PS C:Powershell> Get-Process | Group-Object Company -NoElement Count Name ----- ---- 2 Adobe Systems Incorpor... 52 2 微軟 22 Microsoft Corporation 1 Adobe Systems, Inc. 1 Microsoft (R) Corporation 1 1 NVIDIA Corporation
使用格式化命令分組
Group-Object并不是唯一可以完成分組功能的命令,事實(shí)上格式化命令例如Format-Object支持一個(gè)GroupBy的參數(shù),也可以完成分組。
PS C:Powershell> Dir | Sort-Object Extension, Name | Format-Table -groupBy Extension 目錄: C:Powershell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2011/11/24 20:26 12060 alias 目錄: C:Powershell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2011/11/28 15:30 63 ping.bat 目錄: C:Powershell Mode 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
- PowerShell實(shí)現(xiàn)按條件終止管道的方法
- PowerShell中終止管道的方法
- PowerShell入門教程之PowerShell管道介紹
- Windows Powershell導(dǎo)出管道結(jié)果
- Windows Powershell分析和比較管道結(jié)果
- Windows Powershell過濾管道結(jié)果
- Windows Powershell使用管道
- Windows Powershell 管道和重定向
- PowerShell函數(shù)中接收管道參數(shù)實(shí)例
- PowerShell中使用Filter來創(chuàng)建管道輸入函數(shù)
- PowerShell管道入門必看篇(管道例子大全)
相關(guān)文章
Powershell小技巧之使用-F方法帶入數(shù)據(jù)
這篇文章主要介紹了Powershell的一個(gè)使用-F方法帶入數(shù)據(jù)的小技巧,非常的簡單,也很易懂,記錄一下分享給大家2014-09-09
Windows Powershell Switch 循環(huán)
這篇文章主要介紹了Windows Powershell Switch 循環(huán)以及PowerShell中數(shù)組可以與switch語句結(jié)合,產(chǎn)生意想不到的效果。2014-10-10
基于PowerShell在Ubuntu系統(tǒng)的使用詳解
下面小編就為大家分享一篇基于PowerShell在Ubuntu系統(tǒng)的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
PowerShell函數(shù)用Hash表傳參實(shí)例
這篇文章主要介紹了PowerShell函數(shù)用Hash表傳參實(shí)例,即把Hash表作為PowerShell函數(shù)參數(shù)傳入例子,需要的朋友可以參考下2014-07-07
原創(chuàng)powershell腳本小工具ctracert.ps1跟蹤路由(win8以上系統(tǒng))
這篇文章主要介紹了原創(chuàng)powershell腳本小工具ctracert.ps1跟蹤路由(win8以上系統(tǒng)),需要的朋友可以參考下2017-10-10
Powershell小技巧--遠(yuǎn)程對比服務(wù)配置
這篇文章主要介紹了使用Powershell遠(yuǎn)程對比服務(wù)配置的方法,大家可以推廣下獲取服務(wù)器其他參數(shù)進(jìn)行對比,希望對大家能有所幫助2014-09-09

