我需要计算特定文本文件中的人声、辅音和所有其他字符。
我创建了一个计算人声和辅音的脚本,但我不知道如何计算其余的字符。
目标是计算所有字符,即使它们来自另一种语言,如冰岛字符和逗号、句号和感叹号等符号。
这是我当前的代码:
Clear-Host
$vocal = (Get-Content C:\Users\Administrator\Desktop\POWERSHELL\testfil.txt | Select-String -Pattern "a|e|i|o|u|æ|ø|å" -AllMatches).Matches.Count
$vocal = (Get-Content C:\Users\Administrator\Desktop\POWERSHELL\testfil.txt | Select-String -Pattern "b|c|d|f|g|h|j|k|l|m|n|p|q|r|s|t|v|w|x|z" -AllMatches).Matches.Count
$sign = $sign - $vocal - $consonant
if ($consonant -ge $vocal -ge $sign) {
"`nThere are $vocal vocals, $consonant consonants and $sign other signs in the chosen document.`n"
} else {
break
}
我意识到我需要某种方法来计算字符总数,然后减去人声和辅音(和空格)以找到第三个数字,但我无法弄清楚。
最佳答案
您可以使用 Measure-Object
使用 -Characters
获取文件中的总字符数转变。然后,以下代码将此属性的值返回给变量:
$TotalChars = (Get-Content C:\Users\Administrator\Desktop\POWERSHELL\testfil.txt | Measure-Object -Character).Characters
$sign = $TotalChars - $vocal -$consonant
如果你想打折空白字符,你也可以使用
-ignorewhitespace
与 Measure-Object
切换.
关于powershell - 计算特定文本文件中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728463/