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

2021-11-27  本文已影响0人  yichen_china

很简单的命令:

批量替换字符串

 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 
       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 即可

上一篇 下一篇

猜你喜欢

热点阅读