Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete duplicate string with PowerShell

Tags:

powershell

I have got text file:

1 2 4 5 6 7
1 3 5 6 7 8
1 2 3 4 5 6
1 2 4 5 6 7

Here first and last line are simmilar. I have a lot of files that have double lines. I need to delete all dublicate.

like image 909
Suliman Avatar asked Sep 30 '11 11:09

Suliman


3 Answers

All these seem really complicated. It is as simple as:

gc $filename | sort | get-unique > $output

Using actual file names instead of variables:

gc test.txt| sort | get-unique > unique.txt
like image 92
Alan Jackson Avatar answered Nov 04 '22 13:11

Alan Jackson


To get unique lines:

PS > Get-Content test.txt | Select-Object -Unique
1 2 4 5 6 7
1 3 5 6 7 8
1 2 3 4 5 6

To remove the duplicate

PS >  Get-Content test.txt | group -noelement | `
      where {$_.count -eq 1} | select -expand name

1 3 5 6 7 8
1 2 3 4 5 6
like image 23
Shay Levy Avatar answered Nov 04 '22 13:11

Shay Levy


If order is not important:

Get-Content test.txt | Sort-Object -Unique | Set-Content test-1.txt

If order is important:

$set = @{}
Get-Content test.txt | %{
    if (!$set.Contains($_)) {
        $set.Add($_, $null)
        $_
    }
} | Set-Content test-2.txt
like image 1
Roman Kuzmin Avatar answered Nov 04 '22 15:11

Roman Kuzmin