Windows PowerShell to find duplicate lines in a file

让人想犯罪 __ 提交于 2019-12-10 23:15:07

问题


I need to find the duplicate values in a text file using power shell let's say if the file content is

Apple
Orange
Banana
Orange
Orange

Desired output should be

Orange
Orange

回答1:


You can also use the Group-Object cmdlet to see if any lines occur more than once:

e.g.

Get-Content test.txt | Group-Object | Where-Object { $_.Count -gt 1 } | Select -ExpandProperty Name



回答2:


Used the Commands mentioned below and it worked.

PS C:\Projects> $OriginalContent, $UniqueContent = (Get-Content .\File.txt), (Get-Content .\File.txt | Sort-object -unique)
PS C:\Projects> compare-object $originalcontent $uniquecontent

or to print the text and the count use the following command

PS C:\Projects> Get-Content .\File.txt | group-object


来源:https://stackoverflow.com/questions/43808886/windows-powershell-to-find-duplicate-lines-in-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!