【问题标题】:Find duplicate values in one of the two columns in a text file在文本文件的两列之一中查找重复值
【发布时间】:2017-10-04 20:09:14
【问题描述】:

我有一个文本文件,每行有两个值,用空格分隔。现在我想知道其中一列中的重复值。是否可以使用 Windows powershell 来实现这一点。

给定一个文本文件:

Apple Fruit
Banana Fruit
Carrot Vegetable

期望的输出是:(我想在第二列中找到重复项)

Fruit

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


    【解决方案1】:

    您可以使用Import-CSV cmdlet 并指定一个空格分隔符,以便轻松访问第二列。然后您可以使用第二列分组对象并选择具有超过 1 个条目的对象:

    Import-Csv 'path_to_your_text_file' -Delimiter ' ' -Header @('first', 'second') | 
        Group-Object second | 
        Where-Object count -gt 1 | 
        Select-Object -ExpandProperty name
    

    【讨论】:

    • -header first, second are enought not necessary to use array ;)
    猜你喜欢
    • 2015-06-30
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 2016-08-04
    相关资源
    最近更新 更多