找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 21871|回复: 6

[转载代码] [PowerShell每日技巧]删除重复行(20140304)

[复制链接]
发表于 2014-3-6 12:34:22 | 显示全部楼层 |阅读模式
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
发表于 2014-3-6 15:43:53 | 显示全部楼层
如果翻译过来,我相信会有更多的人有兴趣看的时间更久。
 楼主| 发表于 2014-3-6 16:39:06 | 显示全部楼层
回复 2# ivor


    没空
发表于 2014-4-19 23:07:05 | 显示全部楼层
看来是个行家,我刚接触这一块,还得请您多支持呢!加我好友吧,657307291
 楼主| 发表于 2014-4-20 09:18:14 | 显示全部楼层
回复 4# sdjnms


    不聊QQ,直接在论坛切磋吧。
发表于 2014-4-20 20:01:55 | 显示全部楼层
回复 5# DAIC


   你在线不啊?我这有个多行的word,现在想删除重复项,有什么号办法吗?能教教我吗?
 楼主| 发表于 2014-4-20 20:31:05 | 显示全部楼层
回复 6# sdjnms


    word不是纯文本文件,不方便直接处理。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 01:11 , Processed in 0.016957 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表