Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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] } }'
@oogali
Copy link

oogali commented May 24, 2012

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

@loisaidasam
Copy link
Author

loisaidasam commented May 24, 2012

Ogali ftw

@loisaidasam
Copy link
Author

loisaidasam commented May 24, 2012

Even better:

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

@loisaidasam
Copy link
Author

loisaidasam commented May 24, 2012

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

@loisaidasam
Copy link
Author

loisaidasam commented May 24, 2012

Here it is!

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

@adamazing
Copy link

adamazing commented Oct 9, 2014

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