0

我有以下脚本来获取文件中逗号的数量。

ls | select -First 10 |
% { 
    $_.FullName;
    cat $_ | Select-String `, -AllMatches | 
    Select-Object LineNumber, @{ n = "Count"; e = { $_.Matches.Count }} |
    Group-Object Count
}

该脚本为每个文件返回以下行。如何获得每个文件最常见的逗号计数?对于以下示例,我需要将最多逗号计数77、行数108和总行数 ( 108 + 8 +5 = 121 ) 传递给另一个程序。

文件1.txt

计数名称组                                                                             
----- ---- -----                                                                             
  108 77 {@{LineNumber=1; 计数=77},@{LineNumber=2; 计数=77},@{LineNumber=3; 计数=77...
    8 78 {@{LineNumber=7; 计数=78},@{LineNumber=15; 计数=78},@{LineNumber=22; 数=...
    5 79 {@{LineNumber=16; 计数=79},@{LineNumber=32; 计数=79},@{LineNumber=37; 数数...

文件2.txt
......
4

2 回答 2

1

如果我明白你在问什么,你应该按计数(降序)对 Group 的输出进行排序并选择第一个:

ls | select -First 10 |
% { 
    $_.FullName;
    cat $_ | Select-String `, -AllMatches | 
    Select-Object LineNumber, @{ n = "Count"; e = { $_.Matches.Count }} |
    Group-Object Count | Sort Count -Descending | Select-Object -First 1
}

用逗号测量行数:

ls | select -First 10 |
% { 
    $_.FullName;
    cat $_ | Select-String `, -AllMatches | 
    Select-Object LineNumber, @{ n = "Count"; e = { $_.Matches.Count }} |
    Measure-Object Count
}
于 2013-02-20T00:17:30.247 回答
1

您可以执行以下操作,将相关数据块打包到每个文件的自定义对象中:

ls | select -First 10 |
% { 
    $numLines = 0
    $grouped = cat $_ `
               |%{ $numLines++; $_ }  `
               | Select-String ',' -AllMatches `
               | Select-Object LineNumber, @{ n = "Count"; e = { $_.Matches.Count }} `
               | Group-Object Count `
               | sort Count -Desc

    [PsCustomObject] @{
         File = $_.FullName
         TopCommaCount = [int] $grouped[0].Name
         TopCommaCountLines = [int] $grouped[0].Count
         TotalLines = $numLines
     }
}
于 2013-02-20T00:18:54.257 回答