Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find duplicate values in one of the two columns in a text file

I have a text file with two values in each line separated by a space. Now I want to know duplicate values in one of the columns. Is it possible to achieve this using Windows powershell.

Given a text file:

Apple Fruit
Banana Fruit
Carrot Vegetable

Desired output is: (I want to find duplicates in second column)

Fruit
like image 439
vineel Avatar asked May 06 '17 12:05

vineel


People also ask

How do I find duplicates in a text file?

To start your duplicate search, go to File -> Find Duplicates or click the Find Duplicates button on the main toolbar. The Find Duplicates dialog will open, as shown below. The Find Duplicates dialog is intuitive and easy to use. The first step is to specify which folders should be searched for duplicates.


1 Answers

You could use the Import-CSV cmdlet and specify a whitespace delimiter to easy get access of the second column. Then you can group the objects using the second column and select the one with more than 1 entries:

Import-Csv 'path_to_your_text_file' -Delimiter ' ' -Header @('first', 'second') | 
    Group-Object second | 
    Where-Object count -gt 1 | 
    Select-Object -ExpandProperty name
like image 71
Martin Brandl Avatar answered Sep 28 '22 22:09

Martin Brandl