Windows PowerShell to find duplicate lines in a fi

2019-07-30 15:50发布

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

2条回答
看我几分像从前
2楼-- · 2019-07-30 16:06

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
查看更多
可以哭但决不认输i
3楼-- · 2019-07-30 16:06

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
查看更多
登录 后发表回答