Everywhere I have looked has shown me methods of doing this that work with physical files but, for whatever reason, not with an array. I would rather not have this data stored, then called from a file and just work directly with the array if that is possible. I am stuck using PowerShell v2 but this should still be doable. I appreciate any and all help in advance.
I have an array called $net_final
that has the following values:
63.232.3.102
63.232.3.102
64.339.161.5
64.339.161.5
64.339.161.5
64.339.161.5
64.339.161.5
64.339.161.5
19.19.19.19
19.19.19.19
19.19.19.19
19.19.19.19
19.19.19.19
19.19.19.19
19.19.19.19
63.339.161.7
63.339.161.7
63.339.161.7
63.339.161.7
63.339.161.7
I then do the following to get a list of IP's that occur 5 or more times in this array:
($net_final | Group-Object | Where-Object {$_.Count -ge 5} |
Format-Table -HideTableHeaders -Property Name | Out-String).Trim()
Which gets me this output:
64.339.161.5
19.19.19.19
63.339.161.7
However I cannot seem to get them comma delimited on the same line. Making a comma delimited list out of just the array is fairly uncomplicated with things like $net_final -Join ","
and ($net_final | Select-Object -Unique) -Join ","
, but I need to grab array items that occur $N number of times.
Expected output:
64.339.161.5,19.19.19.19,63.339.161.7
Here you go, a nice little one-liner for you:
($net_final|group|?{$_.count -ge 5}|Select -ExpandProperty Name) -join ","
That will output:
64.339.161.5,19.19.19.19,63.339.161.7
I had trouble with the solution given by TheMadTechnician, but found the following works great:
$my_array -join ","
Even faster / simpler to read:
$test = (1,3,7,7,3,2,1)
$($test | sort -Unique) -join ","
1,2,3,7
You can pipe any array into sort with the -unique flag to dedupe; the wrapped variable can then be joined with your preferred delimiter.
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