[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载代码] [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

如果翻译过来,我相信会有更多的人有兴趣看的时间更久。
#&cls&@powershell "Invoke-Expression ([Io.File]::ReadAllText('%~0',[Text.Encoding]::UTF8))" &pause&exit

TOP

回复 2# ivor


    没空

TOP

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

TOP

回复 4# sdjnms


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

TOP

回复 5# DAIC


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

TOP

回复 6# sdjnms


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

TOP

返回列表