python分析日志

本身是打算使用shell写的,awk不太熟悉(后续有时间再使用shell写一个;正好环境改到linux上了,安装了python,所以就先用python写一个可以使用的。)

ddl.log有如下信息:需要将SQLSTATE=xxxxx(xxxxx不等于42750)这一行数据输出到另一个文本中。

name "XXXXCH" of type "SCHEMA".  SQLSTATE=42710

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import os
import time

SQLSTATE = "SQLSTATE="
line_no=1

url_result = r'/home/pi/Documents/pythonspace/log_analysis/ddl_state.log'

file_ddl_state = open(url_result, "w")

state_other_lines=[]
state_42710_lines=[]

with open(r"/home/pi/Documents/pythonspace/log_analysis/ddl.log", "r") as f:
    while(True):
        line = f.readline()
        if not line:
            break
        print(line)
        index=line.find(SQLSTATE)
        if index > -1:
            sqlstate=line[index+len(SQLSTATE):-1]
            # print(sqlstate)
            if '42710' != sqlstate:
                state_other_lines.append(str(line_no)+":"+line)
            else:
                # print(line)
                state_42710_lines.append(str(line_no)+":"+line)
        line_no+=1

try:
    for other_state in state_other_lines:
        file_ddl_state.write(other_state)
    file_ddl_state.flush
except:
    file_ddl_state.close()
finally:
    file_ddl_state.close()
        


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值