Top

Count website views in Bash script

Sep 29, 2020 | 691 views

#Linux #Bash


count_website_views.sh

First line combine all log files into STDIN stream, then grep certain website host, use double quote as seperator and filter second column as it is a url. Finally, sort it, count it, and sort it again.

zcat /var/log/nginx/access.log*.gz | cat - /var/log/nginx/access.log | \
    grep -E 'https://(www.|)stateofemptiness.com/' | \
    awk -F '"' '{print $2}' | awk '{print $2}' | sort | uniq -c | sort -n 

Filter a certain period of time in access.log

sed -n '/25\/Sep\/2020/,/30\/Sep\/2020/p' /var/log/nginx/access.log
# Or in a less concise way
grep -E $(for i in `seq 25 30`; do echo -n "$i/Sep/2020"; [ $i -ne 30 ] && echo -n "|"; done) /var/log/nginx/access.log



Leave a comment

0 comments