2

次のスクリプトは、2 つの列を返します。ファイル名を表示する列の前に別の列を追加することはできますか?

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) 
            }
        }
}
LineNumber を数える                            
----- -----------                            
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...}                        
4

1 に答える 1

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

于 2013-02-14T23:11:52.460 に答える