13

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
vineel
  • 3,483
  • 2
  • 29
  • 33
  • Should it just return the duplicate if it is on the next line? I see Orange 3 times in the input, and only 2 times in the output. – Sjark May 05 '17 at 15:46
  • 1
    @Sjark, my requirement was to know whether there are any duplicates in a file. So the output can print Orange two or three times works for me. Anyways thanks for looking into it. – vineel May 05 '17 at 15:56

2 Answers2

28

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
Nasir
  • 10,935
  • 8
  • 31
  • 39
  • Thanks Nasir!! That really helped. I am having a similar problem posted can you please check it out. http://stackoverflow.com/questions/43820264/windows-powershell-to-find-duplicate-values-in-one-of-the-two-columns-in-a-text – vineel May 06 '17 at 12:08
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
vineel
  • 3,483
  • 2
  • 29
  • 33