Bash tip: Print begin and end timestamp on apache logs…

This morning I needed to audit some log files that I had recently processed through AWstats and received a report that there was a discrepancy in the data. The complaint was that one day was missing. I used the following bash script to print out the start and end timestamp of each log file:

for file in $( ls -tr *.gz ) ;
  BEGIN=$(zcat ${file} | head -n 1 | awk '{print $4}');
  END=$(zcat ${file} | tail -n 1 | awk '{print $4}');
  echo "${file} - ${BEGIN} - ${END}";

Note that each log file was named uniquely by web server and logrotate number, eg webserver1.access_log.XX.gz.

Leave a Reply

Your email address will not be published. Required fields are marked *