#!/bin/bash ## date conversion from Apache-Logrecord-format -- by Eugene Reimer 2010-03; ## receives date+time+timezone in the form: 06/Mar/2010:01:58:16 -0500 and converts it to any FORMAT supported by date-command; ## USAGE ## datecvt-from-Apache [+FORMAT] ApacheDate[ApacheTime] [ApacheTimezone] --where FORMAT is as in date-cmd -- see man date ## EXAMPLES: ## datecvt-from-Apache +%Y-%m-%dT%H:%M%z 06/Mar/2010:01:58:16 -0500 --produces: 2010-03-06T00:58-0600 (in winter; note timezone was supplied) ## datecvt-from-Apache +%Y-%m-%dT%H:%M%z 06/Mar/2010:01:58:16 --produces: 2010-03-06T01:58-0600 (in winter; note timezone was omitted) if [ $# -eq 0 ];then echo "usage: datecvt-from-Apache [+FORMAT] ApacheDate[ApacheTime] [ApacheTimezone]"; exit; fi FMT="+%Y-%m-%dT%H:%M%z" ##default for FORMAT DT=; for A in "$@";do if [[ $A == +* ]];then FMT="$A"; else DT="$DT$A";fi;done ##handle cmdline args, catenating onto $DT w/o space DT=${DT//\/}; DT=${DT/:/ } ##remove all slashes, replace first colon with space date -d"$DT" "$FMT" ##convert according to $FMT