View previous topic :: View next topic |
Author |
Message |
deprave n00b
Joined: 14 May 2004 Posts: 63 Location: Flint, Michigan
|
Posted: Thu Apr 28, 2011 11:16 pm Post subject: Cat, awk, uniq and sort to count ip # occuranced and list |
|
|
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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10587 Location: Somewhere over Atlanta, Georgia
|
Posted: Thu Apr 28, 2011 11:46 pm Post subject: |
|
|
So provide a snippet of the log file.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10587 Location: Somewhere over Atlanta, Georgia
|
Posted: Thu Apr 28, 2011 11:53 pm Post subject: |
|
|
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 |
|
|
deprave n00b
Joined: 14 May 2004 Posts: 63 Location: Flint, Michigan
|
Posted: Thu Apr 28, 2011 11:57 pm Post subject: |
|
|
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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10587 Location: Somewhere over Atlanta, Georgia
|
Posted: Fri Apr 29, 2011 12:00 am Post subject: |
|
|
"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 |
|
|
|