有一个log.txt。
" [25-Feb-2016 11:27:16 +0200]:登录失败...... 212.153.100.19获取/ .... emailaddress@email.com" ..... ...
我如何编写一个脚本,只能将日期/ IP地址和电子邮件地址grep或regex,并将其写入另一个.txt。
最重要的是我需要日期和相应的IP和电子邮件。
我尝试使用下一个代码,但它是所有数据的分段..
import os
import re
import datetime
filename = 'log.txt'
newfilename = 'output.txt'
if os.path.exists(filename):
data = open(filename,'r')
bulkemails = data.read()
else:
print "File not found."
raise SystemExit
r = re.compile(r'[\w\.-]+@[\w\.-]+\b')
results = r.findall(bulkemails)
emails = ""
for x in results:
emails += str(x)+"\n"
ip = re.compile(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b')
result = ip.findall(bulkemails)
ip =""
for y in result:
ip += str(y)+"\n"
dt = re.compile(r'(\d{4})-(\d{2})-(\d{2})')
result = dt.findall(bulkemails)
dt =""
for z in result:
dt += str(z)+"\n"
def writefile():
f = open(newfilename, 'w')
f.write(emails + ip + dt)
f.close()
print "File written."
def overwrite_ok():
response = raw_input("Are you sure you want to overwrite "+str(newfilename)+"? Yes or No\n")
if response == "Yes":
writefile()
elif response == "No":
print "Aborted."
else:
print "Please enter Yes or No."
overwrite_ok()
if os.path.exists(newfilename):
overwrite_ok()
else:
writefile()
所以我想要相同的output.txt包括下一个:
25-Feb-2016 11:27:16 +0200] - 212.153.100.19 - emailaddress@email.com"
25-Feb-2016 11:27:16 +0200] - 212.153.100.10 - emailaddress1@email.com"
25-Feb-2016 11:27:16 +0200] - 212.153.100.11 - emailaddress2@email.com"
感谢您的帮助,祝您度过愉快的一天:)