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

不羁岁月 提交于 2019-12-23 11:39:10

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/43820264/find-duplicate-values-in-one-of-the-two-columns-in-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!