Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows PowerShell to find duplicate lines in a file

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
like image 609
vineel Avatar asked May 05 '17 15:05

vineel


2 Answers

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
like image 159
Nasir Avatar answered Nov 19 '22 20:11

Nasir


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
like image 1
vineel Avatar answered Nov 19 '22 19:11

vineel