Board logo

标题: [转载代码] [PowerShell每日技巧]删除重复行(20140304) [打印本页]

作者: DAIC    时间: 2014-3-6 12:34     标题: [PowerShell每日技巧]删除重复行(20140304)

Sort-Object has an awesome feature: with the parameter -Unique, you can remove duplicates:
PS C:\> 1,3,2,3,2,1,2,3,4,7 | Sort-Object
1
1
2
2
2
3
3
3
4
7
PS C:\> 1,3,2,3,2,1,2,3,4,7 | Sort-Object -Unique
1
2
3
4
7


This can be applied to object results, as well. Check out this example: it will get you the latest 40 errors from your system event log:
  1. Get-EventLog -LogName System -EntryType Error -Newest 40 | Sort-Object -Property InstanceID, Message | Out-GridView
复制代码
This may be perfectly fine, but depending on your event log, you may also get duplicate entries.
With -Unique, you can eliminate duplicates, based on multiple properties:
  1. Get-EventLog -LogName System -EntryType Error -Newest 40 | Sort-Object -Property InstanceID, Message -Unique | Out-GridView
复制代码
You will no longer see more than one entry with the same InstanceID AND Message.
You can then sort the result again, to get back the chronological order:
  1. Get-EventLog -LogName System -EntryType Error -Newest 40 | Sort-Object -Property InstanceID, Message -Unique | Sort-Object -Property TimeWritten -Descending | Out-GridView
复制代码
So bottom line is: Sort-Objects parameter -Unique can be applied to multiple properties at once.

http://powershell.com/cs/blogs/tips/archive/2014/03/04/eliminating-duplicates.aspx
作者: ivor    时间: 2014-3-6 15:43

如果翻译过来,我相信会有更多的人有兴趣看的时间更久。
作者: DAIC    时间: 2014-3-6 16:39

回复 2# ivor


    没空
作者: sdjnms    时间: 2014-4-19 23:07

看来是个行家,我刚接触这一块,还得请您多支持呢!加我好友吧,657307291
作者: DAIC    时间: 2014-4-20 09:18

回复 4# sdjnms


    不聊QQ,直接在论坛切磋吧。
作者: sdjnms    时间: 2014-4-20 20:01

回复 5# DAIC


   你在线不啊?我这有个多行的word,现在想删除重复项,有什么号办法吗?能教教我吗?
作者: DAIC    时间: 2014-4-20 20:31

回复 6# sdjnms


    word不是纯文本文件,不方便直接处理。




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2