Lets say you wanted to count the number of unique IP addresses hitting your Apache server. It’s very easy to do in a Linux (or compatible) shell. In this tutorial, I’m using Ubuntu server.

First, locate the log file that you want to extract. For example, apache2 log file is located at /var/log/apache2 (depending on your distro). For nginx, the log file is located at /var/log/nginx.

Here I give you the first example on how to extract & count unique IP address in Nginx log file.

Nginx Access Log file

cat access.log | awk '{print $1}' | sort -r | uniq -c | sort -nr

Nginx Error Log file

cat error.log | grep -o 'client: [0-9.]*' | sort -r | uniq -c | sort -nr

Next, is the step on how to extract & count unique IP address from Apache log file.
Apache access & error log file

Apache Access Log file

cat access.log | awk '{print $1}' | sort -r | uniq -c | sort -nr

Apache Error Log file

cat error.log | grep -o 'client [0-9.]*' | sort -r | uniq -c | sort -nr

If you have any other step, you can share with me in the comment section. Hope it helps!

By zam

2 thoughts on “Extract unique IP address from Apache & Nginx log file”
  1. Hi, Thank you very much for this it helped me out a lot, I also added for Apache Access Log file an extra | wc for counting how many unique lines.
    cat access.log | awk ‘{print $1}’ | sort -r | uniq -c | sort -nr | wc

    Thank you hopefully this is useful for someone else also. Thanks again

Any Comments?

This site uses Akismet to reduce spam. Learn how your comment data is processed.