首页 > Web开发 > 详细

Powershell小技巧之去除多余的空格

时间:2016-02-05 21:27:08      阅读:321      评论:0      收藏:0      [点我收藏+]

要去去除多余的空格,请尝试下面正则表达式:

PS> ‘[ Man, it  works!  ]‘ -replace ‘\s{2,}‘, ‘ ‘
[ Man, it works! ] 

你也可以用这个方法转换成固定格式的CSV表格:

PS> (qprocess) -replace ‘\s{2,}‘, ‘,‘
>tobias,console,1,3876,taskhostex.exe
>tobias,console,1,3844,explorer.exe
>tobias,console,1,4292,tabtip.exe

一旦变成CSV格式,你就可以使用ConvertFrom-Csv获取该文本数据的对象:
 

PS> (qprocess) -replace ‘\s{2,}‘, ‘,‘ | ConvertFrom-Csv -Header Name, Session, ID, Pid, Process

Name  : >tobias
Session : console
ID   : 1
Pid   : 3876
Process : taskhostex.exe

Name  : >tobias
Session : console
ID   : 1
Pid   : 3844
Process : explorer.exe

Name  : >tobias
Session : console
ID   : 1
Pid   : 4292
Process : tabtip.exe 
(...)

支持所有PS版本

Powershell小技巧之去除多余的空格

原文:http://www.jb51.net/article/55895.htm

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!