当前位置: 移动技术网 > IT编程>脚本编程>Shell > Windows Powershell排序和分组管道结果

Windows Powershell排序和分组管道结果

2017年12月08日  | 移动技术网IT编程  | 我要评论
使用sort-object和group-object可以对管道结果进行分组。 其实每条命令执行后的结果已经排过序了。例如通过ls 查看文件列表,默认会根据name属性进行

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

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

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

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

给对象和哈希表进行排序

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

ps c:powershell> dir | sort-object length, name -descending, -ascending
sort-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},@{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

对数据进行分组

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

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...}

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

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}

使用表达式分组

如果要查看当前目录的文件,根据文件的大小是否大于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 -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并不是唯一可以完成分组功能的命令,事实上格式化命令例如format-object支持一个groupby的参数,也可以完成分组。

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网