你能用这样的东西吗?
$s = @"
this,is,a
test,,
with,
multiple, commas, to, count,
"@
#convert to string-array(like you normally have with multiline strings)
$s = $s -split "`n"
$s | Select-String `, -AllMatches | Select-Object LineNumber, @{n="Count"; e={$_.Matches.Count}} | Group-Object Count
Count Name Group
----- ---- -----
2 2 {@{LineNumber=1; Count=2}, @{LineNumber=2; Count=2}}
1 1 {@{LineNumber=3; Count=1}}
1 4 {@{LineNumber=4; Count=4}}
如果您不想在组中多次使用“计数”属性,则需要自定义对象。像这样:
$s | Select-String `, -AllMatches | Select-Object LineNumber, @{n="Count"; e={$_.Matches.Count}} | Group-Object Count | % {
New-Object psobject -Property @{
"Count" = $_.Name
"LineNumbers" = ($_.Group | Select-Object -ExpandProperty LineNumber)
}
}
输出:
Count LineNumbers
----- -----------
2 {1, 2}
1 3
4 4