Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Cat, awk, uniq and sort to count ip # occuranced and list
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
deprave
n00b
n00b


Joined: 14 May 2004
Posts: 63
Location: Flint, Michigan

PostPosted: Thu Apr 28, 2011 11:16 pm    Post subject: Cat, awk, uniq and sort to count ip # occuranced and list Reply with quote

I need to count the # of of occurrences of an ip in a log file so it comes out a list like the following, i need to use a one line command to do this using cat, awk, uniq, and sort how do i do this


8 IP NUMBER
10 IP NUMBER

8 IP NUMBER

8 IP NUMBER

8 IP NUMBER
_________________
http://www.migamer.com
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10587
Location: Somewhere over Atlanta, Georgia

PostPosted: Thu Apr 28, 2011 11:46 pm    Post subject: Reply with quote

So provide a snippet of the log file. :wink:

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10587
Location: Somewhere over Atlanta, Georgia

PostPosted: Thu Apr 28, 2011 11:53 pm    Post subject: Reply with quote

And, while I'm at it, moved from Documentation, Tips & Tricks to Portage & Programming. Scripting fits better there.

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
deprave
n00b
n00b


Joined: 14 May 2004
Posts: 63
Location: Flint, Michigan

PostPosted: Thu Apr 28, 2011 11:57 pm    Post subject: Reply with quote

Ok I figured it out, anyone wondering this it is


cat logfile | awk '{print $1}' | sort -n | uniq -c | sort -n

if the ip's are in the first column
_________________
http://www.migamer.com
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10587
Location: Somewhere over Atlanta, Georgia

PostPosted: Fri Apr 29, 2011 12:00 am    Post subject: Reply with quote

"Useless Use of Cat" there.
Code:
<logfile awk '{print $1}' | sort -n | uniq -c | sort -n
is better. Can also be done just with AWK pretty easily:
Code:
#!/usr/bin/awk

{ Count[$1]++ }

END {
    for (i in Count)
        printf "%3d %s\n", Count[i], i
}
:)
- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum