Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Powershell count the occurrence of each word

35 views
Skip to first unread message

T Ton

unread,
Aug 9, 2015, 6:24:06 PM8/9/15
to
Hello,

I have these lines:

Apple
Orange
Banana
Pear
Apple
Banana
Banana

I want to count the occurrence of each word. So, the ouput would be something like these:


2 apple
1 orange
3 banana
1 pear


In UNIX, you can use wc to easily do it. But I don't know how to manipulate it in POWERSHELL.


Help please. Thanks!

Tom

unread,
Aug 10, 2015, 2:47:27 AM8/10/15
to
You can use the cmdlet Group-Object. Group-Object works with arrays.

"Apple", "Orange", "Banana", "Pear", "Apple", "Banana", "Banana" | Group-Object

Count Name Group
----- ---- -----
2 Apple {Apple, Apple}
1 Orange {Orange}
3 Banana {Banana, Banana, Banana}
1 Pear {Pear}


With "Get-Help Group-Object -Full" you get all the information you need (including some examples).

Regards,
Tom
0 new messages