美文网首页
mysqlbinlog日志sql文件利用php正则匹配指定表恢复

mysqlbinlog日志sql文件利用php正则匹配指定表恢复

作者: yichen_china | 来源:发表于2021-11-27 11:49 被阅读0次

很简单的命令:

批量替换字符串

 cat test.txt |tr -s "test" "test1" >ll
#生成新文件
 cat test.txt |tr -s "test" "test1" >  test2.txt

就可以把test.txt中的test字符串,替换成test1

另外删除指定的字符串为:

删除字符串

cat test.txt |tr -d "test" > ll

[Linux是cat、tail、head查看文件任意几行的数据]

一、使用cat、tail、head组合

1、查看最后100行的数据

  cat filename | tail -n 100

2、查看100到300行的数据

  cat filename | head -n 300 | tail -n +100

1、cat filename 打印文件所有内容

2、tail -n 100 打印文件最后100行的数据

        cat filename tail -n 100

3、tail -n +100 打印文件第100行开始以后的内容

       cat filename tail -n +100 
  • 4、head -n 100 打印前100的内容 *
       cat filename head -n 100

php正则替换脚本

b.php

<?php
ini_set("error_reporting","E_ALL & ~E_NOTICE");
$sql_file="471.sql"; //完整的sql文件
$tab_name="`beiyaozhongtai`.`zt_product_base`"; //要提取的表
 
$sql = new SplFileObject($sql_file);
$log=false;
foreach($sql as $line){ //逐行读取
 if ( preg_match("/^(SET TIMESTAMP=)/", $line) ) { //当前行找到匹配
$timestamp=$line;
 }
//   if ( preg_match("/(create|delete|replace|insert|alter|update)([A-Z_\-\.\s\`]+){$tab_name}/i", $line) ) { //当前行找到匹配
 if ( preg_match("/(create|replace|insert|alter|update)([A-Z_\-\.\s\`]+){$tab_name}/i", $line) ) { //当前行找到匹配
file_put_contents($sql_file."_out.sql", $timestamp, FILE_APPEND); //时间戳
$log=true;
 }
 if ($log) {
file_put_contents($sql_file."_out.sql", $line, FILE_APPEND);
 }
 if ( $log && preg_match("/\/\*\!\*\/;/",$line) ){
$log=false;
 }
}
 
echo "导出完毕!\n";

执行 php b.php 即可

相关文章

网友评论

      本文标题:mysqlbinlog日志sql文件利用php正则匹配指定表恢复

      本文链接:https://www.haomeiwen.com/subject/viwfxrtx.html