I have a line like this to extract the datetime per hour of a apache log
awk '{print $4}' elasticsearch.log.* | cut -c2-15 | sed -e 's/$/:00:00/
The problem is that the output date is formatted like below
07/Jul/2014:06:00:00
Is there a way to convert the datetime format on the fly using command line to a more common format like 'YYYY-MM-DD HH:mm:ss' e.g. 2014-07-07 06:00:00 ?
One way I found right now is using an intermediary script
#!/usr/bin/env python
import sys
import re
months = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec' : '12'}
regex = re.compile("(\d{2})/(\w+)/(\d{4}):(\d{2}):(\d{2}):(\d{2})",re.IGNORECASE)
for line in sys.stdin:
try:
r = regex.search(line)
g = r.groups()
print g[2] + '-' + months[g[1]] + '-' + g[0] + ' ' + g[3] + ':' + g[4] + ':' + g[5]
except:
pass
but i'm looking if there is a much shorter way