1

I'm trying to use Powershell to search a csv file and output a list of duplicate lines in a csv file. I can accomplish this pretty easily in bash with the following:

uniq -d myfile.csv > list.csv

In Powershell I can output a list of unique lines but how do I modify Get-Unique to display only the duplicate lines like I did in bash?

Get-Content c:\file\myfile.csv | Get-Unique | Set-Content c:\file\list1.csv

1 Answer 1

2

It's a bit weird to use the unique tool to get the duplicates. How about:

gc .\test.csv | group -NoElement |? Count -gt 1 | select -expand name

This groups the lines by how many there are, identifies the ones with duplicates, and outputs them. e.g. if:

test.csv contains:

a,b,c
d,e,f
a,b,c
z,z,z

gc test.csv | group

Count Name                      Group                                                                    
----- ----                      -----                                                                    
    2 a,b,c                     {a,b,c, a,b,c}                                                           
    1 d,e,f                     {d,e,f}                                                                  
    1 z,z,z                     {z,z,z}                                                                  
    1                           {}                                                                       

and -NoElement stops it building the group contents, redundant in this case.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That's exactly what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.