Windows PowerShell 查找文件中的重复行

Windows PowerShell to find duplicate lines in a file

我需要使用 power 在文本文件中查找重复值 shell 假设文件内容是

Apple
Orange
Banana
Orange
Orange

期望的输出应该是

Orange
Orange

使用了下面提到的命令并且有效。

PS C:\Projects> $OriginalContent, $UniqueContent = (Get-Content .\File.txt), (Get-Content .\File.txt | Sort-object -unique)
PS C:\Projects> compare-object $originalcontent $uniquecontent

或使用以下命令打印文本和计数

PS C:\Projects> Get-Content .\File.txt | group-object

您还可以使用 Group-Object cmdlet 查看是否有任何行出现不止一次:

例如

Get-Content test.txt | Group-Object | Where-Object { $_.Count -gt 1 } | Select -ExpandProperty Name