How to Extract Unique IP Addresses from an Apache Logfile

This extracts unique IP addresses from a logfile. You can use it for SEO or analytics.

[shell]cat log | cut -d’ ‘ -f1 | sort | uniq -u >unique_ips[/shell]

Here’s how it works:

  • cat — this prints the contents of a file
  • | — the pipe symbol sends the output of the previous command into the next command
  • cut -d' ' -f1 — this cuts the file along a delimiter, which is a space (-d=' ') and keeps only the first field (-f1). In an Apache log, that is the IP address column.
  • sort — the output needs to be sorted before you can get the unique lines
  • uniq -u — this keeps only the unique lines
Unique IPs
The output (pixelated for privacy)

To learn more about the commands, type man cat (or whatever command you want information on) in a terminal.

Tested on Ubuntu 11.10.

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *