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?
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.
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.
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.
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
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With