Hey, PowerShell Guy ! How Can I Tally Up All the Words Found in a Text File?
And an other Hey Scripting guy ! translation to PowerShell
How Can I Tally Up All the Words Found in a Text File?
Hey, Scripting Guy! While browsing the Internet I found a script that showed me how to get a list of all the unique words in a text file. That’s useful, but I’d like to go one step further: how can I determine the number of times each of those words occurs??
In PowerShell those 2 articles are very simple to do,we also use the split to get an array of words, already a bit easer on PowerShell
but after that we have the great PowerShell object tools as select, group and sort so from there on it is a .. piece of "Cake-Meel" :
$s = "I saw the cat. The cat was black."
# Remove Chars
",",".","!","?",">","<","&","*","=","`n" |% {$s = $s.replace($_,' ')}
# Array of words, spaces removed
$w = $s.Split() |? {$_.Length -gt 0 }
# Unique words
$w | select -Unique
# Tally
$w | group
# Sort and format
$W | group | sort name | ft name,count -AutoSize
You can see for the count we just can switch for select -unique to group :
This looks like this :
PoSH> $s = "I saw the cat. The cat was black."
PoSH>
PoSH> # Remove Chars
PoSH>
PoSH> ",",".","!","?",">","<","&","*","=","`n" |% {$s = $s.replace($_,' ')}
PoSH> $w = $s.Split() |? {$_.Length -gt 0 }
PoSH>
PoSH> # Unique words
PoSH>
PoSH> $w | select -Unique
I
saw
the
cat
The
was
black
PoSH>
PoSH> # Tally
PoSH>
PoSH> $w | group
Count Name Group
----- ---- -----
1 I {I}
1 saw {saw}
2 the {the, The}
2 cat {cat, cat}
1 was {was}
1 black {black}
PoSH>
PoSH> # Sort and format
PoSH>
PoSH> $W | group | sort name | ft name,count -AutoSize
Name Count
---- -----
black 1
cat 2
I 1
saw 1
the 2
was 1
PoSH>
Does almost look too simpe .. Not ?
Also this account for The and the being the same word .
Enjoy,
Greetings /\/\o\/\/