I have the following script to get the counts of commas in files.
ls | select -First 10 |
% {
$_.FullName;
cat $_ | Select-String `, -AllMatches |
Select-Object LineNumber, @{ n = "Count"; e = { $_.Matches.Count }} |
Group-Object Count
}
The script returns the following rows for each file. How do I get the most common comma counts for each file? As for the following example, I will need to pass the most comma count 77 , the line count 108 and total lines ( 108 + 8 +5 = 121 ) to another program.
File1.txt Count Name Group ----- ---- ----- 108 77 {@{LineNumber=1; Count=77}, @{LineNumber=2; Count=77}, @{LineNumber=3; Count=77... 8 78 {@{LineNumber=7; Count=78}, @{LineNumber=15; Count=78}, @{LineNumber=22; Count=... 5 79 {@{LineNumber=16; Count=79}, @{LineNumber=32; Count=79}, @{LineNumber=37; Count... File2.txt .....
If I understand what you're asking, you should sort the output of Group by Count (descending) and select the first one:
ls | select -First 10 |
% {
$_.FullName;
cat $_ | Select-String `, -AllMatches |
Select-Object LineNumber, @{ n = "Count"; e = { $_.Matches.Count }} |
Group-Object Count | Sort Count -Descending | Select-Object -First 1
}
To measure the number of lines with a comma:
ls | select -First 10 |
% {
$_.FullName;
cat $_ | Select-String `, -AllMatches |
Select-Object LineNumber, @{ n = "Count"; e = { $_.Matches.Count }} |
Measure-Object Count
}
You could do something like this, packaging the relevant pieces of data into a custom object for each file:
ls | select -First 10 |
% {
$numLines = 0
$grouped = cat $_ `
|%{ $numLines++; $_ } `
| Select-String ',' -AllMatches `
| Select-Object LineNumber, @{ n = "Count"; e = { $_.Matches.Count }} `
| Group-Object Count `
| sort Count -Desc
[PsCustomObject] @{
File = $_.FullName
TopCommaCount = [int] $grouped[0].Name
TopCommaCountLines = [int] $grouped[0].Count
TotalLines = $numLines
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.