从MySql二进制文件中取多行

我有MySQL的binlog,我需要输出一定的时间框架到一个单独的文件,我该怎么做?

以下是binlog文件包含的示例:

# at 460 #130120 0:09:17 server id 1 end_log_pos 487 Xid = 79514636 COMMIT/*!*/; # at 487 #130120 0:09:17 server id 1 end_log_pos 560 Query thread_id=248447 exec_time=0 error_code=0 

我期待grep以下:

 #130120 0:09:17 server id 1 end_log_pos 487 Xid = 79514636 COMMIT/*!*/; # at 487 

我试过pcregrep -M ,但到目前为止没有任何运气,我的正则expression式技能不是我认为的那些地方,这里是我的实际行:

 # mysqlbinlog /var/lib/mysql/log/logbin/mysql-bin.001036 | pcregrep -M '130120(\n|.*)\ at\ ' # 

*更新*

  • 不同查询之间的行数有所不同。

*更新2 *

这实际上做了这个工作…

 # mysqlbinlog /var/lib/mysql/log/logbin/mysql-bin.001036 | sed -e '/130120 13/,/ at /!d' > /tmp/13 # 

真正的答案是使用命令行选项到mysqlbinlog

mysqlbinlog --start-datetime=datetime --stop-datetime=datetime /path/to/mysql-bin.001036

这可以用awk很容易地完成。

例如

 mysqlbinlog mysql-bin.001 | awk '($1 == "130120") {print $0}' > results.txt 

这告诉awk在第一列中find与130120 完全匹配的130120 ,然后打印整行。

如果你需要一分一秒,那么你可以做如下的事情:

 awk '(($1 == "130120") && ($2 == "0:09:17")) {print $0}' 

如果你只是需要一分钟,你可以使用这样的东西:

 awk '(($1 == "130120") && ($2 ~ "^0:09:")) {print $0}' 

这样做的工作:

 # mysqlbinlog /var/lib/mysql/log/logbin/mysql-bin.001036 | sed -e '/130120 13/,/ at /!d' > /tmp/13 # 
 grep -C 1 COMMIT filename 

-C是上下文,前一行和后一行

-A在之后

-B在之前