# -*- 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
一凡 閱讀(295)
評論(0) 編輯 收藏 所屬分類:
linux