免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2960 | 回复: 14
打印 上一主题 下一主题

黑哥,请进,我需要你的帮助!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-07 11:27 |只看该作者 |倒序浏览
本帖最后由 侧面bt 于 2010-07-07 12:00 编辑

现在要把错误日志文件里面的sql语句提出来,组合成正确的重装执行,日志文件的格式如下:

[time] 2010-07-05 10:45:54.765432
[tid] 6308
[file] ../src/DBOpr.cpp
[line] 38
[info] DB Error: Fail To Execute SQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UpdatePeroid = 0 where id= 1164605496' at line 1
SQL: update table_29 set dname = 'sunsecond.com', IPNum = 1, IP = '114.80.162.3',  UpdateTime = '2010-07-05 10:45:54' UpdatePeroid = 0 where id = 116460549

[time] 2010-07-05 10:45:57.229553
[tid] 6310
[file] ../src/DBOpr.cpp
[line] 38
[info] DB Error: Fail To Execute SQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UpdatePeroid = 0 where id= 14204116719' at line 1
SQL: update table_21 set dname = '15aaa.com', IPNum = 3, IP = '173.244.202.183
n173.244.202.184
173.244.202.189'
UpdateTime = '2010-07-05 10:45:57' UpdatePeroid = 0 where id = 1420411671

现在的问题是对于ip个数是1个的倒还好处理,因为都在一行内,但对于IP有多个的,由于IP这个字段是换\n分隔的,所以在日志文件里变成了多行,还在要还原为一行,比如第一条记录要还原成:
update table_21 set dname = '15aaa.com', IPNum = 3, IP = '173.244.202.183\n173.244.202.184\n173.244.202.189' UpdateTime = '2010-07-05 10:45:57' UpdatePeroid = 0 where id = 142041167

请问黑哥该怎么处理啊,用什么好,awk还是sed还是其它方法,谢了,对了,日志记录非常大,大概有10万条以上。

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
2 [报告]
发表于 2010-07-07 11:33 |只看该作者
回复 1# 侧面bt


    大家都能帮忙啊。你这样写不好。

论坛徽章:
0
3 [报告]
发表于 2010-07-07 11:41 |只看该作者
回复 1# 侧面bt


      多贴点日志,黑哥才能帮你

论坛徽章:
0
4 [报告]
发表于 2010-07-07 11:49 |只看该作者
  1. $ cat a
  2. nfo] DB Error: Fail To Execute SQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQ
  3. L server version for the right syntax to use near 'UpdatePeroid = 0 where DomainHash = 1420411671916276923' at line 1
  4. SQL: update table_21 set dname = '15aaa.com', IPNum = 3, IP = '173.244.202.183
  5. n173.244.202.184
  6. 173.244.202.189'
  7. UpdateTime = '2010-07-05 10:45:57' UpdatePeroid = 0 where id = 1420411671

  8. $ sed  '/IP/{:a;N;/UpdateTime/!ba;s/\n/ /g;}' a
  9. nfo] DB Error: Fail To Execute SQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQ
  10. L server version for the right syntax to use near 'UpdatePeroid = 0 where DomainHash = 1420411671916276923' at line 1
  11. SQL: update table_21 set dname = '15aaa.com', IPNum = 3, IP = '173.244.202.183 n173.244.202.184 173.244.202.189' UpdateT
  12. ime = '2010-07-05 10:45:57' UpdatePeroid = 0 where id = 1420411671
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-07-07 12:13 |只看该作者
谢谢楼上的,可以解释一下吗,看不太懂。

论坛徽章:
0
6 [报告]
发表于 2010-07-07 12:32 |只看该作者
  1. awk '/^SQL/{printf "%s ",substr($0,index($0,"update"));while(getline && $0 !~ /^$/)printf "%s ",$0;printf "\n\n"}' file
复制代码

论坛徽章:
0
7 [报告]
发表于 2010-07-07 12:38 |只看该作者
回复 5# 侧面bt


    sed  '/IP/{:a;N;/UpdateTime/!ba;s/\n/ /g;}'
匹配到IP所在行,从这行开始,把下面的行都读到模式空间里,直到匹配到UpdateTime为止,以上做的,实际上就是把所有包含IP地址的行读进准备处理,以下的就是处理了:把换行符替换为一个空格。

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
8 [报告]
发表于 2010-07-07 12:57 |只看该作者
回复 1# 侧面bt


    如果是gawk:
  1. awk -v RS='at line[^\n]*\nSQL:|\n[ \t\n]*\n' '!(NR%2){$1=$1;print}' urfile
复制代码

论坛徽章:
0
9 [报告]
发表于 2010-07-07 13:53 |只看该作者
我也写一个~
awk 'BEGIN{RS=""}{gsub(/.*SQL: |\n/," ");print}' test5.txt

论坛徽章:
0
10 [报告]
发表于 2010-07-07 15:55 |只看该作者
怎么你的sql没有加上逗号,你应该先修改好应用程序才来做这些脚本吧。


我觉得还是先按模式grep出来这堆sql,然后用vi慢慢整理,

避免你用批处理又搞出其他问题来了。

建议:开始搞之前先备份一下数据库表。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP