使用 powershell 计算文件中的行长度

Count line lengths in file using powershell

如果我有一个长文件,其中包含许多长度不同的行,我如何计算每行长度的出现次数?

示例:

this
is
a
sample
file
with
several
lines
of
varying
length

输出:

Length Occurences
1      1
2      2
4      3
5      1
6      2
7      2

你有什么想法吗?

怎么样

get-content <file> | Group-Object -Property Length | sort -Property Name

根据文件的长度,您可能想要做一些更有效率的事情

对于大批量工作,请使用 Get-Content-ReadCount

$ht = @{}
 Get-Content <file> -ReadCount 1000 |
 foreach {
  foreach ($line in $_)
   {$ht[$line.length]++}
}

$ht.GetEnumerator() | sort Name