Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - filtering for unique values

Tags:

I have an input CSV file with a column containing information similar to the sample below:

805265 995874 805674 984654 332574 339852 

I'd like to extract unique values into a array based on the leading two characters, so using the above sample my result would be:

80, 99, 98, 33

How might I achieve this using PowerShell?

like image 387
Temple Avatar asked Mar 22 '12 15:03

Temple


People also ask

How do I Sort unique values in PowerShell?

There are three ways to do this: use the Get-Unique cmdlet, use the Unique parameter from the Sort-Object cmdlet, or use the Unique parameter from the Select-Object cmdlet.

How do I remove duplicates in PowerShell output?

Use Get-Unique to Remove Duplicates From PowerShell Array It compares each item in a sorted list to the next item, removes duplicates, and returns only one member of each item. This cmdlet only works if the list is sorted. You can use the Sort-Object cmdlet to sort objects by property values.

How do I get the type of a variable in PowerShell?

There are different data types exist for the variable like Byte, Int32, Float, String, etc. To get the variable type, we need to use the GetType() method.


2 Answers

Use Select-Object and parameter -unique:

$values = '805265', '995874', '805674', '984654', '332574', '339852'  $values |   Foreach-Object { $_.Substring(0,2) } |   Select-Object -unique 

If conversion to int is needed, then just cast it to [int]:

$ints =   $values |   Foreach-Object { [int]$_.Substring(0,2) } |   Select-Object -unique 
like image 150
stej Avatar answered Sep 29 '22 01:09

stej


I'd use the Group-Object cmdlet (alias group) for this:

 Import-Csv foo.csv | group {$_.ColumnName.Substring(0, 2)}  Count Name                      Group ----- ----                      -----     2 80                        {805265, 805674}     1 99                        {995874}     1 98                        {984654}     2 33                        {332574, 339852} 
like image 21
Keith Hill Avatar answered Sep 29 '22 01:09

Keith Hill