Powershell create a comma delimited list from array of IP's that occur $N number of times

85,973

Solution 1

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

Solution 2

I had trouble with the solution given by TheMadTechnician, but found the following works great:

$my_array -join ","

Solution 3

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.

Share:
85,973
beardedeagle
Author by

beardedeagle

I am a freelance software development engineer with thirteen years of experience in the industry, the last seven of which have been spent with GoDaddy. With a passion for automation, monitoring, coding and new technologies I am always looking for ways to improve processes in place as well as myself.

Updated on February 18, 2020

Comments

  • beardedeagle
    beardedeagle about 4 years

    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

  • beardedeagle
    beardedeagle about 10 years
    Thank you so much. Works like a charm. I cannot believe I was so close to the answer and didn't realize it. Frustrating, but again thank you very much.
  • Ring
    Ring about 9 years
    Worked for me as well. Thanks.
  • TheMadTechnician
    TheMadTechnician about 9 years
    The problem with this is that it will give all IP's joined by commas, not just unique ones, and the original post only wanted it where there were 5 or more duplicates which this doesn't account for either. Your code returns all 15 IP addresses joined with commas.
  • TheMadTechnician
    TheMadTechnician about 7 years
    This has the same issue as the answer by mgood1. This gathers all unique entries, where the requester only wanted items that repeat a certain number of times.