Skip to content

Instantly share code, notes, and snippets.

@loisaidasam
Created May 23, 2012 09:59
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save loisaidasam/2774350 to your computer and use it in GitHub Desktop.
Save loisaidasam/2774350 to your computer and use it in GitHub Desktop.
One liner for counting unique IP addresses from nginx logs
# One liner for counting unique IP addresses from nginx logs
# Feel free to comment with better ideas - I'm sure it's not the best way of doing this (I'm no awk ninja!)
#
# Sample output:
#
# $ cat example.com.access.log | awk -F " " '{a[$1]++ } END { for (b in a) { print b, "\t", a[b] } }'
# 66.65.145.220 49
# 92.63.28.68 126
cat example.com.access.log | awk -F " " '{a[$1]++ } END { for (b in a) { print b, "\t", a[b] } }'
@loisaidasam
Copy link
Author

Ogali ftw

@loisaidasam
Copy link
Author

Even better:

cat example.com.access.log | cut -f1 -d ' ' | uniq -c | sort -g -r

@loisaidasam
Copy link
Author

On second thought, that's not right...

@loisaidasam
Copy link
Author

Here it is!

$ cat example.com.access.log | cut -f1 -d ' ' | sort | uniq -c | sort -g -r

@adamazing
Copy link

Can be rewritten with the UUoC ommitted :
<example.com.access.log cut -f1 -d ' ' | sort | uniq -c | sort -g -r

Or:
cut -f1 -d ' ' example.com.access.log | sort | uniq -c | sort -g -r

@Fazel94
Copy link

Fazel94 commented Nov 17, 2015

I have log file, sized 1.7 GB,
does these solutions work for that???

@fthiery
Copy link

fthiery commented Sep 18, 2017

@Fazel94 hell yes

@thieryl
Copy link

thieryl commented May 27, 2019

awk '{print $1 " " $3 " " $4}' access.log| sort | uniq -c | sort -nr
will give you the time frame

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment