windows Powershell 从出现 $N 次的 IP 数组中创建一个逗号分隔的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23417896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:33:11  来源:igfitidea点击:

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

arrayswindowssortingpowershellpowershell-2.0

提问by beardedeagle

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.

我看过的所有地方都向我展示了处理物理文件的方法,但无论出于何种原因,不适用于数组。我宁愿不存储这些数据,然后从文件中调用,如果可能的话,直接使用数组。我一直在使用 PowerShell v2,但这应该仍然可行。我提前感谢任何和所有帮助。

I have an array called $net_finalthat has the following values:

我有一个名为的数组$net_final,它具有以下值:

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:

然后,我执行以下操作以获取在此数组中出现 5 次或更多次的 IP 列表:

($net_final | Group-Object | Where-Object {$_.Count -ge 5} | Format-Table -HideTableHeaders -Property Name | Out-String).Trim()

($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.

但是我似乎无法在同一行上用逗号分隔它们。制作一个逗号分隔的列表出来只是阵列的是喜欢的东西相当简单$net_final -Join ","($net_final | Select-Object -Unique) -Join ",",但我需要出现的次数$ N多抢数组项。

Expected output:

预期输出

64.339.161.5,19.19.19.19,63.339.161.7

64.339.161.5,19.19.19.19,63.339.161.7

回答by TheMadTechnician

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

回答by mgood1

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

我在TheMadTechnician给出的解决方案上遇到了麻烦,但发现以下方法很好用:

$my_array -join ","

$my_array -join ","

回答by user3166350

Even faster / simpler to read:

阅读速度更快/更简单:

$test = (1,3,7,7,3,2,1)
$($test | sort -Unique) -join "," 

1,2,3,7

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.

您可以使用 -unique 标志将任何数组通过管道进行排序以进行重复数据删除;然后可以将包装的变量与您首选的分隔符连接起来。