Reputation: 29189
I'm using the following script to get the comma counts.
Get-Content .\myFile |
% { ($_ | Select-String `, -all).matches | measure | select count } |
group -Property count
It returns,
Count Name Group ----- ---- ----- 131 85 {@{Count=85}, @{Count=85}, @{Count=85}, @{Count=85}...} 3 86 {@{Count=86}, @{Count=86}, @{Count=86}}
Can I show the line number in the Group
column instead of @{Count=86}, ...
?
The files will have a lot of lines and majority of the lines have the same comma. I want to group them so the output lines will be smaller
Upvotes: 0
Views: 1747
Reputation: 54911
Can you use something like this?
$s = @"
this,is,a
test,,
with,
multiple, commas, to, count,
"@
#convert to string-array(like you normally have with multiline strings)
$s = $s -split "`n"
$s | Select-String `, -AllMatches | Select-Object LineNumber, @{n="Count"; e={$_.Matches.Count}} | Group-Object Count
Count Name Group
----- ---- -----
2 2 {@{LineNumber=1; Count=2}, @{LineNumber=2; Count=2}}
1 1 {@{LineNumber=3; Count=1}}
1 4 {@{LineNumber=4; Count=4}}
If you don't want the "count" property multiple times in the group, you need custom objects. Like this:
$s | 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)
}
}
Output:
Count LineNumbers
----- -----------
2 {1, 2}
1 3
4 4
Upvotes: 3