Results 1 to 4 of 4
I have written a quick shell command to go through the output of an apache log and pull out the IP addresses and the number of times they occur. The ...
- 02-04-2011 #1Just Joined!
- Join Date
- Feb 2011
- Posts
- 2
Bash script to pull and count IP addresses out of an apache log
I have written a quick shell command to go through the output of an apache log and pull out the IP addresses and the number of times they occur. The IP addresses appear in the 2nd column of the log.
for i in `awk '{print $2}' logfile | sort -u`; do echo $i " " `grep -c $i logfile`; done
This provides the desired output, but takes a while to run against a big file. It also feels a bit of a brute force method.
Does anyone have a more elegant solution that won't take hours on a large file as my shell scripting is kind of self taught and I may be missing something obvious to someone else!
Thanks
- 02-05-2011 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
One important idea is to "touch" the data as few times as possible. Using the pipeline and appropriate options on commands to isolate the data, then sort it, then count it in as few passes as possible would be something like this:
I think the awk will be fast enough, but you could compare it with a version using cut.Code:awk '{print $2}' logfile | sort | uniq -c
See man pages for details.
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 02-08-2011 #3Just Joined!
- Join Date
- Feb 2011
- Posts
- 2
Cheers for that.
Much appreciated.
- 08-14-2011 #4Just Joined!
- Join Date
- Aug 2011
- Posts
- 5
try something like this,
if you want to send the information to a text file instead of sending the output to the screen, just remove the pound sign.Code:#!/bin/bash cat /var/www/html | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | tr " " "\n" | uniq -c # >> apachelogips.txt


Reply With Quote