经常需要在 binlog 中查找一些日志信息,于是写了一个简单的脚本。对于非常巨大的 binlog 文件,该脚本可能会速度慢,毕竟还是用的 list,暂时没想到好办法。

详细看代码:

#/usr/bin/python

#2016-04-12

#search string in the binlogs

#usage:
#put this file into binlog-dir,exec as:
#"python test.py 111 123 update" or
#"python test.py 111 123 update and insert" or
#"python test.py 111 123 update or delete"
#the nums are the binlog-num. import sys
import subprocess def find_str(files):
for file in files:
comm = "mysqlbinlog {0}".format(file)
lines = subprocess.Popen(comm, shell=True, stdout=subprocess.PIPE)
lines = lines.stdout.readlines()
for line in lines:
line = line.lower()
if len(sys.argv) == 4:
if sys.argv[3] + ' ' in line:
yield line
elif len(sys.argv) == 6 and sys.argv[4] == "and":
if sys.argv[3] + ' ' in line and sys.argv[5] + ' ' in line:
yield line
elif len(sys.argv) == 6 and sys.argv[4] == "or":
if sys.argv[3] + ' ' in line or sys.argv[5] + ' ' in line:
yield line if __name__ == "__main__":
start = sys.argv[1]
end = sys.argv[2]
files = ["updatelog.{0:06d}".format(i) for i in range(int(start), int(end)+1)] f = find_str(files)
for i in f:
print(i)

第二次改进版本,由于 py2 不支持 yield from 语句,gen_concatenate() 可能有点绕;详细看代码:

#/usr/bin/python

#--

#search string in the binlogs

#usage:
#put this file into binlog-dir,exec as "python test.py 111 123 update" or "python test.py 111 123 update and insert" or "python test.py 111 123 update or delete"
#the nums are the binlog-num. import sys
import subprocess def find_str(files):
print(sys.argv)
for file in files:
comm = "mysqlbinlog {0}".format(file)
lines = subprocess.Popen(comm, shell=True, stdout=subprocess.PIPE)
lines = lines.stdout.readlines()
yield lines #此处返回的是生成器对象 def gen_concatenate(lines):
  #将多个生成器对象迭代返回
for i in lines:
for it in i:
yield it def gen_grep(lines):
for line in lines:
line = line.lower()
if len(sys.argv) == :
if sys.argv[] + ' ' in line:
yield line
elif len(sys.argv) == and sys.argv[] == "and":
if sys.argv[] + ' ' in line and sys.argv[] + ' ' in line:
yield line
elif len(sys.argv) == and sys.argv[] == "or":
if sys.argv[] + ' ' in line or sys.argv[] + ' ' in line:
yield line if __name__ == "__main__":
start = sys.argv[]
end = sys.argv[]
files = ["updatelog.{0:06d}".format(i) for i in range(int(start), int(end)+)] f = find_str(files)
lines = gen_concatenate(f)
greplines = gen_grep(lines)
for i in greplines:
print(i)

脚本1理解起来更加容易,将实现功能全部封装在一个函数体内;

更新:

使用了 re 正则匹配,有时候日志里面记录的表名是带反引号的,比如`user`这样,见代码:

#/usr/bin/python

#2016-04-27

#search string in the binlogs

#usage:
#put this file into binlog-dir,exec as "python test.py 111 123 update" or "python test.py 111 123 update and insert" or "python test.py 111 123 update or delete"
#the nums are the binlog-num. import sys
import subprocess
import re def find_str(files):
print(sys.argv)
for file in files:
comm = "mysqlbinlog {0}".format(file)
lines = subprocess.Popen(comm, shell=True, stdout=subprocess.PIPE)
lines = lines.stdout.readlines()
yield lines def gen_concatenate(lines):
for i in lines:
for it in i:
yield it def gen_grep(lines):
for line in lines:
line = line.lower()
if len(sys.argv) == 4:
# if sys.argv[3] + ' ' in line:
# yield line
str1 = sys.argv[3]
str1_match = re.search(str1, line, re.I)
if str1_match is not None:
yield line
elif len(sys.argv) == 6 and sys.argv[4] == "and":
# if sys.argv[3] + ' ' in line and sys.argv[5] + ' ' in line:
# yield line
str1 = sys.argv[3]
str2 = sys.argv[5]
if re.search(r'\b{}\b'.format(str1), line, re.I) is not None and re.search(r'\b{}\b'.format(str2), line, re.I):
yield line
elif len(sys.argv) == 6 and sys.argv[4] == "or":
# if sys.argv[3] + ' ' in line or sys.argv[5] + ' ' in line:
# yield line
str1 = sys.argv[3]
str2 = sys.argv[5]
if re.search(r'\b{}\b'.format(str1), line, re.I) is not None and re.search(r'\b{}\b'.format(str2), line, re.I):
yield line if __name__ == "__main__":
start = sys.argv[1]
end = sys.argv[2]
files = ["updatelog.{0:06d}".format(i) for i in range(int(start), int(end)+1)] f = find_str(files)
lines = gen_concatenate(f)
greplines = gen_grep(lines)
for i in greplines:
print(i) search.py

search.py

Python 查找binlog文件的更多相关文章

  1. python查找读写文件

    import os ''' 跟据文件名称,后缀查找指定文件 path:传入的路径 filename:要查找的文件名 suffix:要查找的文件后缀 return :返回查找的文件路径 ''' file ...

  2. Python查找指定文件

    在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径: import os testfiles = [] testfilepaths = [] L = len(os.p ...

  3. 【转】Python查找某文件夹下的所有excel文件

    # -*- coding: utf-8 -*- # author:baoshan import os dirname = r'D:\0.shenma\01.聊城资料\01.数据资料\02.聊城年鉴数据 ...

  4. Python实现对文件夹内文本文件递归查找

    平台:Win7 64 bit,IDLE Python 3.4.0 经常有这样的需求:在一个文本文件里查找特定字符串,这很好实现,用任何文本查看工具几乎都可以做到.而有的时候,想查找一个文件夹下的所有文 ...

  5. python 查找文件内容

    输入查找的文件夹路径,要查找的内容关键字(可以指定多个),要查找的文件类型(可以是多个),搜索出符合条件的文件,并记录所有符合条件的行号及行内容. 写的感觉有点冗余,但好歹还能使用^-^,主要是方便手 ...

  6. python实现在目录中查找指定文件的方法

    python实现在目录中查找指定文件的方法 本文实例讲述了python实现在目录中查找指定文件的方法.分享给大家供大家参考.具体实现方法如下: 1. 模糊查找 代码如下: import os from ...

  7. 使用二分法查找mobile文件中区号归属地

    #!/usr/bin/env python #coding:utf-8 ''' Created on 2015年12月8日 @author: DL @Description: 使用二分法查找mobil ...

  8. Python操作Zip文件

    Python操作Zip文件 需要使用到zipfile模块 读取Zip文件 随便一个zip文件,我这里用了bb.zip,就是一个文件夹bb,里面有个文件aa.txt. import zipfile # ...

  9. python自定义库文件路径

    各有各的小烦恼,各有的小期待 这是人家私事,不要大嘴巴 在Pycharm中import whois时,总是失败 原因是安装了python3.x相关操作过程,将环境变量path中关于Python的配置c ...

随机推荐

  1. 前端环境安装(node.js+npm+grunt+bower)

    前端开发环境安装(本教程不带开发工具的安装教程,只是环境安装) 本人机器环境win7 64位. 一.node.js安装 进入官网下载node.js文件,http://www.nodejs.org/ 2 ...

  2. C#中“类似GridView等控件”的前台显示与后台数据变化之间的关系

    最近用dev的treelist,gridcontrol等控件,这些控件显示数据都需要进行DataTable等数据源的绑定,而经理又要求可以随时更改其中的内容,刚开始总是不断的刷新控件.更新控件的数据源 ...

  3. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  4. Spring之ContextLoaderListener的作用

    Spring org.springframework.web.context.ContextLoaderListener public class ContextLoaderListener exte ...

  5. python密码强口令检测

    主要就是输入判断检测,以及一些正则的学习.刚开始玩python项目,代码写的不好.我以前玩C的!! 代码的价值与其是否能够实现功能等价! #密码输入检测 密码长度不少于8个字符,而且必须包含大写/小写 ...

  6. zTree模糊查询人员姓名:getNodesByParamFuzzy

    以前使用zTree,不知道有getNodesByParamFuzzy这个模糊查询的方法,所以用了比较笨的方法,比如:功能要求(模糊查询人员姓名),先获得用户输入的名称,然后到数据库或者缓存中去查找,然 ...

  7. 在Eclipse中安装testNG插件

    1. 选择菜单:Help->Install New Software,点击Add按钮输入框中输入相应的Name:testNG和Location:http://beust.com/eclipse. ...

  8. openstack私有云布署实践【14.2 登录页dashboard-controller(办公网环境)】

    这一小节基本配置相同,但留意以下紫色部份的配置,当初为了管理方便,我们让办公网openstack的dashboard的登录桥接了科兴的dashboard,由此统一dashboard界面的登录地址   ...

  9. jdbc连接数据库的步骤

    1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的静态方法forName(String  classN ...

  10. input的type属性

    input的type属性:http://www.w3school.com.cn/tags/att_input_type.asp 基本语法: <input type="hidden&qu ...