ca9163d9
ca9163d9

Reputation: 29159

Powershell group by multiple fields

The following script will returns two columns. Is it possible to add another column in front of the columns showing the file names?

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...}                        

Upvotes: 2

Views: 2434

Answers (1)

Frode F.
Frode F.

Reputation: 54881

Try this(untested):

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) 
            }
        }
}

If you want filepath instead of only filename, change the 3rd line to $filename = $_.FullName

Upvotes: 4

Related Questions