以下脚本将返回两列。是否可以在显示文件名的列前添加另一列?
ls | select -First 10 |
% {
cat $_ | 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 ----- ----------- 77 {1, 2, 3, 4...} 78 {7, 15, 22, 43...} 79 {16, 32, 37, 90...} 77 {1, 2, 3, 4...} 78 {7, 15, 22, 43...} 79 {16, 32, 37, 90...} 77 {1, 2, 3, 4...} 78 {7, 15, 22, 43...} 79 {16, 32, 37, 90...} 77 {1, 2, 3, 4...} 78 {7, 15, 22, 43...} 79 {16, 32, 37, 90...} 77 {1, 2, 3, 4...} 78 {7, 15, 22, 43...} 79 {16, 32, 37, 90...} 77 {1, 2, 3, 4...} 78 {7, 15, 22, 43...} 79 {16, 32, 37, 90...} 89 {1, 2, 3, 4...} 89 {1, 2, 3, 4...} 89 {1, 2, 3, 4...} 89 {1, 2, 3, 4...}
答案 0 :(得分:4)
试试这个(未经测试):
ls | select -First 10 |
% {
$filename = $_.Name
cat $_ | Select-String `, -AllMatches |
Select-Object LineNumber, @{n="Count"; e={$_.Matches.Count}} |
Group-Object Count |
% {
New-Object psobject -Property @{
"FileName" = $filename
"Count" = $_.Name
"LineNumbers" = ($_.Group | Select-Object -ExpandProperty LineNumber)
}
}
}
如果您想要文件路径而不是文件名,请将第3行更改为$filename = $_.FullName