python 查找日志关键字
1、抓取出含有关键字”xiaoming”的行
2、在上一个问题的基础上,假设所在行的格式为location=xiaoming, value=xxx,请筛选出value值
#!/usr/bin/python3 # -*- coding: utf-8 -* import re # 定义日志文件 log_file = '/var/logs/test.log' f = open(log_file, 'r+') lines = f.readlines() def print_log(): # 查找关键字 for line in lines: if "xiaoming" in line: line1 = line print(line1) # 查找value的值 if 'location=xiaoming, value=' in line1: value = re.findall(r'value=(\w+)*', line1, re.M) print(value) print_log() f.close()