Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell. count the number of duplicate entries in a file

Tags:

powershell

I have a text file with a list of IP addresses, and i need to count the number of times the same IP appears in the file.

I've found lots of ways of removing duplicate entries, and how to count the number of lines, but nothing on how to counting duplicate entries.

I haven't got much of a script yet, not anything worth posting, but if someone can point me in the right direction, that will be a start.

like image 871
mattnicola Avatar asked Oct 17 '25 16:10

mattnicola


1 Answers

If $file contains

10.1.1.2  
10.1.1.2  
10.1.1.2  
10.1.1.10  
10.1.1.10  
10.1.1.20

then

Get-Content $file | Group-Object -noelement

Count Name                     
----- ----                     
3     10.1.1.2                 
2     10.1.1.10                
1     10.1.1.20 
like image 186
mattnicola Avatar answered Oct 20 '25 06:10

mattnicola



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!