RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Extract unique IP addresses from an apache log

Question:

Hi how do I extract all IP addresses from my httpd log. I need to extract only unique IP addresses from my apache log file.

Here is a my sample apache log entry:

XXX.64.70.XXX - - [26/Mar/2011:00:28:23 -0700] "GET / HTTP/1.1" 403 4609 
"-" "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like 
Gecko) Chrome/10.0.648.204 Safari/534.16"

Answer:

Upon the apache log entry format you have supplied, the easiest way to extract in IP addresses from this kind of apache log entries is to use a combination of awk, sort and uniq commands. First we need to get a long list of IP addresses. This can be done with awk command:

 

$ awk '{ print $1 } ' apache_log

Next step is to sort IP addresses with sort command:

$ awk '{ print $1 } ' apache_log | sort 

And finally we get only unique IP address by streaming the output of the above command to uniq:

$ awk '{ print $1 } ' apache_log | sort | uniq

This will create a long list of short IP addresses, each on separate line. If you intend to count unique visitors on your page simply redirect the output to wc -l.

$ awk '{ print $1 } ' apache_log | sort | uniq | wc -l 

Linux questions and answers

Share this linux post:

Submit Extract unique IP addresses from an apache log in Delicious Submit Extract unique IP addresses from an apache log in Digg Submit Extract unique IP addresses from an apache log in FaceBook Submit Extract unique IP addresses from an apache log in Google Bookmarks Submit Extract unique IP addresses from an apache log in Stumbleupon Submit Extract unique IP addresses from an apache log in Technorati Submit Extract unique IP addresses from an apache log in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download