批处理之家's Archiver

DAIC 发表于 2014-3-6 12:34

[PowerShell每日技巧]删除重复行(20140304)

Sort-Object has an awesome feature: with the parameter -Unique, you can remove duplicates:
[table=100%,black][tr][td][color=white]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
[/color][/td][/tr][/table]

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:[code]Get-EventLog -LogName System -EntryType Error -Newest 40 | Sort-Object -Property InstanceID, Message | Out-GridView[/code]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:[code]Get-EventLog -LogName System -EntryType Error -Newest 40 | Sort-Object -Property InstanceID, Message -Unique | Out-GridView[/code]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:[code]Get-EventLog -LogName System -EntryType Error -Newest 40 | Sort-Object -Property InstanceID, Message -Unique | Sort-Object -Property TimeWritten -Descending | Out-GridView[/code]So bottom line is: Sort-Objects parameter -Unique can be applied to multiple properties at once.

[url=http://powershell.com/cs/blogs/tips/archive/2014/03/04/eliminating-duplicates.aspx]http://powershell.com/cs/blogs/tips/archive/2014/03/04/eliminating-duplicates.aspx[/url]

ivor 发表于 2014-3-6 15:43

如果翻译过来,我相信会有更多的人有兴趣看的时间更久。

DAIC 发表于 2014-3-6 16:39

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=145365&ptid=29127]2#[/url] [i]ivor[/i] [/b]


    没空[img]http://bbs.bathome.net/images/smilies/default/titter.gif[/img]

sdjnms 发表于 2014-4-19 23:07

看来是个行家,我刚接触这一块,还得请您多支持呢!加我好友吧,657307291

DAIC 发表于 2014-4-20 09:18

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=147255&ptid=29127]4#[/url] [i]sdjnms[/i] [/b]


    不聊QQ,直接在论坛切磋吧。

sdjnms 发表于 2014-4-20 20:01

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=147261&ptid=29127]5#[/url] [i]DAIC[/i] [/b]


   你在线不啊?我这有个多行的word,现在想删除重复项,有什么号办法吗?能教教我吗?

DAIC 发表于 2014-4-20 20:31

[b]回复 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=147280&ptid=29127]6#[/url] [i]sdjnms[/i] [/b]


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

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.