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.
Here's my current code:
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
If you want to discount the whitespace characters you can also use the -ignorewhitespace
switch with Measure-Object
.
Assuming you don't want to count whitespace characters you could use a pattern \S
to match single non-whitespace characters for calculating the total character count. You may also want to use character classes instead of alternations, and avoid reading the input file multiple times.
$txt = Get-Content 'C:\path\to\input.txt'
$all = ($txt | Select-String -Pattern '\S' -AllMatches).Matches.Count
$vocal = ($txt | Select-String -Pattern '[aeiouæøå]' -AllMatches).Matches.Count
$consonant = ($txt | Select-String -Pattern '[bcdfghjklmnpqrstvwxz]' -AllMatches).Matches.Count
Mark Wragg's answer worked perfectly.
All i really needed was the ".characters" phrase.
Here's the finished code:
clear-host
$vocal=(Get-Content C:\Users\Administrator\Desktop\POWERSHELL\testfil.txt | Select-String -Pattern "a|e|i|o|u|æ|ø|å" -AllMatches).matches.count
$consonant=(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
$TotalChars=(Get-Content C:\Users\Administrator\Desktop\POWERSHELL\testfil.txt | Measure-Object -ignorewhitespace -Character).Characters
$sign = $TotalChars - $vocal -$consonant
if($consonant -ge $vocal -ge $sign){"`nThere are $vocal vocals, $consonant consonants and $sign other characters in the chosen document.`n"}
else{break}
Note: this assignment was originally made in the Danish language, hence the "æ ø å" in the vocal category. If anyone else needs to do similar scripts, I apologize if I've left some strange words or letters in the script ;)