PowerShell's equivalent to the uniq utility, the Get-Unique cmdlet, unfortunately has no equivalent to the former's -c option for prepending the number of consecutive duplicate lines (as of PowerShell v6.2).
Note: Enhancing Get-Unique to support a -c-like feature and other features offered by the uniq POSIX utility is the subject of this feature request on GitHub.
Therefore, you must roll your own solution:
function Get-UniqueWithCount {
begin {
$instanceCount = 1; $prevLine = $null
}
process {
if ($_ -eq $prevLine) {
++$instanceCount
} elseif ($null -ne $prevLine) {
[pscustomobject] @{ InstanceCount = $instanceCount; Line = $prevLine }
$instanceCount = 1
}
$prevLine = $_
}
end {
[pscustomobject] @{ InstanceCount = $instanceCount; Line = $prevLine }
}
}
The above function accepts input from the pipeline (object by object as $_ in the process { ... } block).
It compares each object (line) to the previous one and, if they're equal, increments the instance count; once a different line is found, the previous line is output, along with its instance count, as an object with properties InstanceCount and Line. The end { ... } block outputs the final output object for the last block of identical consecutive lines.
See about_Functions_Advanced.
Then invoke it as follows:
Get-Content fule | Get-UniqueWithCount
which yields:
InstanceCount Line
------------- ----
3 1
4 0
3 1
1 0
2 1
3 0
1 1
1 0
3 1
2 0
Since Get-UniqueWithCount conveniently outputs objects whose typed properties we can act on, the equivalent of sort -hr (sort by embedded numbers (-h), in descending (reverse) order (-r)) is easy:
Get-Content fule | Get-UniqueWithCount | Sort-Object -Descending InstanceCount
which yields:
InstanceCount Line
------------- ----
4 0
3 1
3 1
3 0
3 1
2 1
2 0
1 0
1 1
1 0
[regex]::Matches('aaaaaaaaaaaaabbbbbbbbccc', '(.)\1+').Groups | Where-Object { $_.Length -gt 1 } | Sort-Object -Unique -Property Valuecombined with[RegexOptions]::Multilineoption for your task.Measure-Objectcommand might be useful too. I'm not sure about your input data size and how fast regular expressions will work.uniq -cdoesn't exclude single instances, so your solution won't work. In general, future readers benefit most from full-fledged answers, not (half-)solutions in comments