log.txtがあります。Python log.txtが気に入っています。 grep/regex
"[25-Feb-2016 11:27:16 +0200]:ログインに失敗しました.... 212.153.100.19 Get/.... [email protected]" ........
日付/ IPアドレスと電子メールアドレスだけをgrepまたはregexして他の.txtに書き出すスクリプトを作成するにはどうすればよいですか。
最も重要なことは、日付と対応するIPと電子メールが必要なことです。
だから私は、次が含まれているものと同じoutput.txtとしたいとwhould
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\.-][email protected][\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()
..私は次のコードでそれをしようとしたが、それは、データのすべてのセグメントである:
25 -Feb-2016 11:27:16 +0200] - 212.153.100.19 - [email protected] "
25-2月-2016 11:27:16 +0200] - 212.153.100.10 - emailaddress1 @ email.com "
2016年2月11日11 :27:16 +0200] - 212.153.100.11 - [email protected]」の助けを
おかげで、素敵な一日を過ごす:)
あなたは3つのグループのための1で正規表現をしなければならない