[mdlug] script question
Michael Corral
micorral at comcast.net
Sat Jan 25 21:22:43 EST 2014
Just got around to reading my email, here's my version:
-------------------------------------------------------------------
#!/bin/bash
LOG="/home/USER/var/DOMAIN/logs/transfer.log"
declare -A ampm
for hh in {00..11}; do ampm[$hh]="AM"; done
for hh in {12..23}; do ampm[$hh]="PM"; done
echo "Unique Hits - Hour"
for hh in {00..23}
do
printf "%-11d - %s\n" $(awk "/2014:${hh}:/ {print \$1}" ${LOG} | \
sort -u | wc -l) "${hh}:00 ${ampm[$hh]}"
done
-------------------------------------------------------------------
Awk can match patterns, so an extra call to grep is unnecessary.
Michael
2014-01-09, Monsieur Mat Enders a ecrit:
> 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
More information about the mdlug
mailing list