I have a text file with 105,779 lines containing duplicates. I need to remove the duplicates without changing the order. I am able to remove the duplicates, but it is changing the order of the text file. Any help would be appreciated.
I have a text file with 105,779 lines containing duplicates. I need to remove the duplicates without changing the order. I am able to remove the duplicates, but it is changing the order of the text file. Any help would be appreciated.
The Select-Object cmdlet has a -Unique switch that does just what you want:
Get-Content allMethods.txt | Select-Object -Unique > uniqueMethods.txt
Note (written as of PowerShell 7.2.3):
While Select-Object -Unique is surprisingly slow, it still outperforms your own manual solution.
Unlike PowerShell in general, -Unique is surprisingly and invariably case-sensitive:
'a', 'a', 'b' | Select-Object -Unique # -> 'a', 'b'
'a', 'A', 'b' | Select-Object -Unique # !! -> 'a', 'A', 'b'