# -*- coding: utf-8 -*-
#!/usr/bin/python
import re
import io
import sys
# obj = re.compile(r'(?P<ip>.*?)- - \[(?P<time>.*?)\] "(?P<request>.*?)" (?P<status>.*?) (?P<bytes>.*?) "(?P<referer>.*?)" "(?P<ua>.*?)"')
# example:xxxx"id":2640914,"orderId":144115188137125591xxxx"state":10xxxxx"
# 日志整行都需要匹配,需要用的用具体正则匹配,如(\d{7}),不需要的用(.*)匹配,总之所有需要或不需要部分都用()括起来
obj = re.compile(r'(.*"id":)(\d{7})(.*"orderId":)(\d{18})(.*"state":)(\d{2})(.*)')
def load_log(path):
# 读取文件
with io.open(path, mode="r", encoding="utf-8") as f:
for line in f:
line = line.strip()
parse(line)
def stdin():
# 读取管道输入
for line in sys.stdin:
parse(line)
def parse(line):
# 解析单行nginx日志
try:
result = obj.match(line)
print(result.group(2,4,6))
except:
pass
if __name__ == '__main__':
# load_log("/tmp/227.log")
stdin()
posted on 2020-02-29 02:10
一凡 阅读(286)
评论(0) 编辑 收藏 所属分类:
linux