[mdlug] script question

Mat Enders mat.enders at gmail.com
Thu Jan 9 06:22:06 EST 2014


I wrote this script to tell me the # of unique hits per hour to a website.
 So clients say look pingdom says my site was down for 14hrs we can no you
were getting hits.  Here is what I wrote, it also works as a one-liner (how
I originally wrote it) if you replace the ;'s with &&.  There has to be a
better way to right this than the way I did it.  All suggestions must use
bash only no perl.

#!/bin/bash

grep '2014:00:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Midnight/' > uhitshr;
grep '2014:01:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - One AM/' >> uhitshr;
grep '2014:02:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Two AM/' >> uhitshr;
grep '2014:03:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Three AM/' >> uhitshr;
grep '2014:04:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Four AM/' >> uhitshr;
grep '2014:05:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Five AM/' >> uhitshr;
grep '2014:06:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Six AM/' >> uhitshr;
grep '2014:07:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Seven AM/' >> uhitshr;
grep '2014:08:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Eight AM/' >> uhitshr;
grep '2014:09:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Nine AM/' >> uhitshr;
grep '2014:10:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Ten AM/' >> uhitshr;
grep '2014:11:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Eleven AM/' >> uhitshr;
grep '2014:12:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Noon/' >> uhitshr;
grep '2014:13:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - One PM/' >> uhitshr;
grep '2014:14:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Two PM/' >> uhitshr;
grep '2014:15:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Three PM/' >> uhitshr;
grep '2014:16:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Four PM/' >> uhitshr;
grep '2014:17:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Five PM/' >> uhitshr;
grep '2014:18:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Six PM/' >> uhitshr;
grep '2014:19:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Seven PM/' >> uhitshr;
grep '2014:20:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Eight PM/' >> uhitshr;
grep '2014:21:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Nine PM/' >> uhitshr;
grep '2014:22:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Ten PM/' >> uhitshr;
grep '2014:23:' /home/USER/var/DOMAIN/logs/transfer.log |awk '{print $1}'
|sort |uniq |wc -l |sed 's/$/ - Eleven PM/' >> uhitshr;
cat uhitshr |awk 'BEGIN {print "Unique Hits - Hour"}{printf "%-11s %-s %-s
%-s\n", $1, $2, $3, $4}' && rm uhitshr


-- 
Mathew E. Enders

"Where once Samba and Apache sold Linux to the world they are now just part
of the plumbing.  But that's OK, plumbers make good money."
--Jeremy Allison


More information about the mdlug mailing list