4

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.

2
  • 1
    You really haven't given us much info. What does the file look like, just a list of IP's, one per line? If so you want Group-Object. Run with that, and come back when you have a script that we can see and help with if you have issues. Commented Apr 7, 2015 at 5:07
  • That was just the start i needed thanks. Commented Apr 7, 2015 at 5:14

1 Answer 1

11

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Most of my logs have timestamps, is there a way i can ignore the first part (timestamp) of each line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.