我有以下脚本来获取文件中的逗号计数。
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 < / em>)到另一个程序。
File1.txt Count Name Group ----- ---- ----- 108 77 {@{LineNumber=1; Count=77}, @{LineNumber=2; Count=77}, @{LineNumber=3; Count=77... 8 78 {@{LineNumber=7; Count=78}, @{LineNumber=15; Count=78}, @{LineNumber=22; Count=... 5 79 {@{LineNumber=16; Count=79}, @{LineNumber=32; Count=79}, @{LineNumber=37; Count... File2.txt .....
答案 0 :(得分:1)
如果我理解你在问什么,你应该按照计数(降序)对输出进行排序并选择第一个:
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
}
答案 1 :(得分: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
}
}