我需要获取一个linux日志文件并将数据输入到mysql表中,并将每个字段分成列。我一直在尝试使用LOAD DATA INFILE命令。唯一的问题是,数据没有分隔,我不能终止FIELDS BY'',因为该文件在日志消息中包含空格。如果有人有任何想法,请告诉我
档案示例:
2011-01-17 10:18:20用户xxxxxx请求备份xxxxx(xxxxx)
2011-01-17 10:18:29停止当前充满xxxxxx,请求backuppc(backoff =)
2011-01-17 10:18:33用户xxxxxx请求备份xxxxxx(xxxxxx)
2011-01-17 10:18:51备份在xxxxxx上取消(收到信号= ALRM)
2011-01-17 10:18:52在xxxxxx上开始完全备份(pid = xxxxxx,share = xxxxxx $)
2011-01-17 10:24:18开始在xxxxxx上完全备份(pid = xxxxxx,share = xxxxxx $)
2011-01-17 11:00:01下次唤醒是2011-01-17 12:00:01
2011-01-17 12:00:03下次唤醒是2011-01-17 13:00:00
2011-01-17 13:00:01下次唤醒是2011-01-17 14:00:00
答案 0 :(得分:1)
预先格式化此文件。我假设你的文件中没有空行(否则你可以使用注释代码删除它们)。假设您正在使用python:
parsed = open("parsed.txt", 'a')
with open("log.txt") as f:
for i, line in enumerate(f):
# use if your lines have spaces in between
# if i % 2 == 0:
parsed.write(line.replace(" ",",",2))
为您提供如下文件:
2011-01-17,10:18:20,User xxxxxx requested backup of xxxxx (xxxxx)
2011-01-17,10:18:52,Started full backup on xxxxxx (pid=xxxxxx , share=xxxxxx$)
2011-01-17,10:24:18,Started full backup on xxxxxx (pid=xxxxxx , share=xxxxxx$)
...
现在你可以:
LOAD DATA INFILE 'parsed.txt' INTO TABLE db.my_table;