Search code examples
powershellscriptingcharactercounting

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.

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.


Solution

  • You can use Measure-Object to get the total characters in a file by using the -Characters switch (Note: in PowerShell 7 this switch is named -Character).

    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.