awk/nawk/gawk is unsuitable for this task. You need to use something like Perl or Python
For the purposes of demonstration I have trimmed the Apache log file (infile) down to
Code:
99.60.97.205 - - [26/Mar/2011:06:00:00 +0000] GET
72.234.67.132 - - [26/Mar/2011:09:00:00 +0000] GET
68.12.178.167 - - [26/Mar/2011:09:30:00 +0000] GET
128.186.145.53 - - [26/Mar/2011:10:00:00 +0000] GET
174.253.212.250 - - [27/Mar/2011:05:00:00 +0000] GET
99.60.97.205 - - [27/Mar/2011:04:00:00 +0000] GET
72.234.67.132 - - [27/Mar/2011:05:00:00 +0000] GET
76.91.142.36 - - [27/Mar/2011:06:00:00 +0000] GET
68.12.178.167 - - [27/Mar/2011:06:30:10 +0000] GET
128.186.145.53 - - [27/Mar/2011:09:00:00 +0000] GET
The following Python code with convert the time field to UTC - 0700
Code:
#!/usr/bin/python
from datetime import datetime
from pytz import timezone
import time
import pytz
import re
utc = pytz.utc
pacific = timezone("US/Pacific")
log_re = '(?P<ip>[.\d]+)(\s+)-(\s+)-(\s+)\[(?P<time>.+)\](\s+)GET'
pattern = re.compile(log_re)
for line in open("infile"):
# print line
m = pattern.match(line)
if m is None:
print "ERROR"
continue
res = m.groupdict()
dt = time.strptime(res["time"][:-6], "%d/%b/%Y:%H:%M:%S")
utc_dt = datetime(dt[0], dt[1], dt[2], dt[3], dt[4], dt[5], tzinfo=utc)
tz_dt = utc_dt.astimezone(pacific)
print res["ip"] + "\t-\t -\t[" + tz_dt.strftime("%d/%b/%Y:%H:%M:%S %z") + "]\tGET"
Here is the output
Code:
99.60.97.205 - - [25/Mar/2011:23:00:00 -0700] GET
72.234.67.132 - - [26/Mar/2011:02:00:00 -0700] GET
68.12.178.167 - - [26/Mar/2011:02:30:00 -0700] GET
128.186.145.53 - - [26/Mar/2011:03:00:00 -0700] GET
174.253.212.250 - - [26/Mar/2011:22:00:00 -0700] GET
99.60.97.205 - - [26/Mar/2011:21:00:00 -0700] GET
72.234.67.132 - - [26/Mar/2011:22:00:00 -0700] GET
76.91.142.36 - - [26/Mar/2011:23:00:00 -0700] GET
68.12.178.167 - - [26/Mar/2011:23:30:10 -0700] GET
128.186.145.53 - - [27/Mar/2011:02:00:00 -0700] GET
To include the remaining parts of the log file, you need to extend log_re to handle the various patterns and
then add the additional elements to the output print statement. I leave that and better error handling to you.
I need to convert the given datetime from PST to UTC, i gone through multiple forum examples but everywhere it mentioned about converting the server datetime. So could someone help me on this.
I will be getting arguments for my script in the format of yyyymmddhh (eg:2015101004 - it will be pst),... (4 Replies)
Hello everyone,
Can you please help me with this problem?
I want to change the timezone of my Solaris 10 system. I found on internet that I should do the following steps:
1- Edit the /etc/TIMEZONE file
2- restart the system : init 6
My question is : In the file /etc/TIMEZONE I should... (5 Replies)
Hi,
how to calculate the time difference between PST date and PDT date in perl scripting.
date1: Mon Dec 31 16:00:01 PST 2015
date2: Tue Mar 19 06:09:30 PDT 2013
and also difference between PST-PST and PDT-PDT
need difference in months or days (months prefereble). (3 Replies)
I have a file which is tab delimited, it contains the GMT date.
Please tell me how to convert from GMT to PST time and if the date and time is of 2 days ago from the current date store the lines in a file or else remove the line.
10.1.10.178 - - -
10.1.10.178 - - ... (2 Replies)
Hi
I'm trying to change my sys clock from PST to UTC.
I've read the man date page
it helpfully says :-
-u, --utc, --universal
print or set Coordinated Universal Time
as root I have tried date --universal, date -u, date --utc,
I have checked the /etc/sysinfo/clock file the... (5 Replies)