0

我正在使用以下脚本来获取逗号计数。

Get-Content .\myFile | 
% { ($_ | Select-String `, -all).matches | measure | select count } | 
group -Property count

它返回,

计数名称组
----- ---- -----
  131 85 {@{Count=85}、@{Count=85}、@{Count=85}、@{Count=85}...}
    3 86 {@{Count=86},@{Count=86},@{Count=86}}

我可以在Group列中显示行号而不是@{Count=86}, ...吗?

这些文件将有很多行,并且大多数行都有相同的逗号。我想对它们进行分组,这样输出线会更小

4

1 回答 1

3

你能用这样的东西吗?

$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 
于 2013-02-14T18:28:23.500 回答