0

I am a beginner to python and I have a log file which I need to take date time. I use regex for taking the 2 conditions but unfortunately my result is not as expected, this is the result I got:

 Date               Time       
 20170119        193739188+0900

log file:

20170119 193739188+0900 elim1td001p imapserv 58124 72559 139941478487808 Note;AcctBadPswd(50/6)

I would like to know that how to change the date and time format inside the regex code to have a better result? This is my regex code:

import re
from csv import writer
log_file = '/Users/kiya/Desktop/mysql/ipscan/ip.txt'
output_file = '/Users/kiya/Desktop/mysql/ipscan/output.csv'

name_to_check = 'MBX_AUTHENTICATION_FAILED'

with open(log_file,encoding="utf-8") as infile:
    for line in infile:
        if name_to_check in line:
            username = re.search(r'(?<=userName=\[)(.*)(?=\],)', line)
            username = username.group()

            date = re.search('(?P<year>\d{4})(?P<month>\d{2})(?P<date>\d{2})', line)
            date = date.groups()

            time = re.search(r'(\d{9}\+\d{4})', line)
            time = time.group()

            ip = re.search(
                r'(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])',
                line)
            ip = ip.group()

            with open(output_file, 'w') as outfile:
            csv_writer = writer(outfile)
            csv_writer.writerow(["Username","Date","Time","Ip_address"])
            csv_writer.writerow([username,date,time,ip])

I would like the result to be as:

Date: 2017-01-09
Time: 01:15:30 (like)
2
  • You just want the time to be the string 'UTC'? Commented May 23, 2018 at 4:29
  • @chrisz not the string UTC, but in UTC format, like: 11:30:45 Commented May 23, 2018 at 4:38

2 Answers 2

1

Using re and datetime module.

Demo:

import re
import datetime
s = "20170119 193739188+0900 elim1td001p imapserv 58124 72559 139941478487808 Note;AcctBadPswd(50/6)"
m = re.search("(?P<date>\d{8})\s+(?P<time>\d{9})\+(?P<zone>\d{4})", s)
if m:
    date = datetime.datetime.strptime(m.group('date'), "%Y%m%d").strftime("%Y-%m-%d")
    time = datetime.datetime.strptime(m.group('time'), "%H%M%S%f").strftime("%H:%M:%S")
    print(date)
    print(time)

Output:

2017-01-19
19:37:39
Sign up to request clarification or add additional context in comments.

3 Comments

but when I try to write it into csv is date is not working
no error, when I do it to csv, the answer is not showing correctly, only it shows the date and time format from the log file
Try this csvwriter.writerow([date,time])
0

Date Parsing can be done like shown below:

>>> import re
>>> line = r'20170119 193739188+0900 elim1td001p imapserv 58124 72559 139941478487808 Note;AcctBadPswd(50/6)'
>>>
>>> matchObj = re.search('(?P<year>\d{4})(?P<month>\d{2})(?P<date>\d{2})\s+(?P<hour>\d{2})(?P<min>\d{2})(?P<sec>\d{2})', line)
>>>
>>> matchObj.groups
<built-in method groups of _sre.SRE_Match object at 0x0000021C1DF1CB20>
>>> matchObj.groups()
('2017', '01', '19', '19', '37', '39')
>>>
>>>
>>> print('Date: %s-%s-%s' % (matchObj.group('year'), matchObj.group('month'), matchObj.group('date')))
Date: 2017-01-19
>>>

Similarly, You can use for time.

>>>
>>> time = 'Time: {}:{}:{}'.format(matchObj.group('hour'), matchObj.group('min'), matchObj.group('sec'))
>>>
>>> time
'Time: 19:37:39'
>>>

1 Comment

when I try to write it into csv is date is not working .i updated my code please have a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.