计算特定文本文件中的字符 [英] Counting characters in a specific text file
问题描述
我需要计算特定文本文件中的人声,辅音和所有其他字符。
I need to count the vocals, consonants and all other characters in a specific text file.
我创建了一个计算人声和辅音的脚本,但是我可以不知道如何计算其余字符。
I created a script that counts the vocals and consonants, but I can't figure out how to count the rest of the characters.
目标是计算所有字符,即使它们来自另一种语言,例如冰岛字符和逗号,句号和感叹号等符号。
The goal is to count ALL characters, even if they're from another language, like Icelandic characters and signs like comma, full stop and exclamation mark.
这是我当前的代码:
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
}
我知道我需要一些方法计算字符总数,然后减去人声和辅音(和空格)以找到第三个数字,但我不知道。
I realize I need some way to count the total amount of characters, and then subtract the vocals and consonants (and spaces) to find the third number, but I can't figure it out.
推荐答案
您可以使用测量对象
使用-字符
开关获取文件中的总字符。然后,以下代码将此属性的值返回到变量:
You can use Measure-Object
to get the total characters in a file by using the -Characters
switch. The following then returns the value of this property to a variable:
$TotalChars = (Get-Content C:\Users\Administrator\Desktop\POWERSHELL\testfil.txt | Measure-Object -Character).Characters
$sign = $TotalChars - $vocal -$consonant
如果要打折空白字符,还可以使用 -ignorewhitespace
开关测量对象
。
If you want to discount the whitespace characters you can also use the -ignorewhitespace
switch with Measure-Object
.
这篇关于计算特定文本文件中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!