如何从 db2 错误日志中 grep 下面的 sql 部分

Posted

技术标签:

【中文标题】如何从 db2 错误日志中 grep 下面的 sql 部分【英文标题】:how to grep below sql part from db2 error log 【发布时间】:2016-12-05 18:48:11 【问题描述】:

我有一个从 db2diag.log 中提取错误消息的脚本。我必须从下面的文件中提取导致死锁的 SQL 查询。

文件内容:log.txt

db2inst1 , WSCOMUSR , MESSAGE : ADM5501I  DB2 is performing lock escalation. The affected application 
          is named "db2jcc_application", and is associated with the workload 
          name "SYSDEFAULTUSERWORKLOAD" and application ID 
          "173.10.105.33.59586.13011817552"  at member "0". The total number of 
          locks currently held is "1249935", and the target number of locks to 
          hold is "624967". The current statement being executed is "delete 
           from DMEXPLOG where CREATED < ? ". Reason code "1"


db2inst1 , WSCOMUSR , MESSAGE : ADM5501I  DB2 is performing lock escalation. The affected application 
          is named "db2jcc_application", and is associated with the workload 
          name "SYSDEFAULTUSERWORKLOAD" and application ID 
          "173.10.105.33.59586.13011817552"  at member "0". The total number of 
          locks currently held is "1249935", and the target number of locks to 
          hold is "624967". The current statement being executed is "select 
          * from DMEXPLOG where CREATED < ?". Reason code "1"

需要的输出:所有的 sql 查询

1. delete 
           from DMEXPLOG where CREATED < ?
2. select 
          * from DMEXPLOG where CREATED < ?

像这样。我想要文件中的所有 sql 部分。有任何grep 或 Awk/sed 解决方案来获得所需的输出吗?

平台:Unix (AIX)

【问题讨论】:

【参考方案1】:
awk 'gsub(/^.*The current statement being executed is \"|\". Reason code.*$/,""); print NR". "$0' log.txt
1. delete from DMEXPLOG where CREATED < ? 
2. 
3. select * from DMEXPLOG where CREATED < ?

匹配的字符串无疑可以更短,并且 2. 是空的,因为您提供的数据在实际数据之间有一个空行。实际数据中有空行吗?

【讨论】:

【参考方案2】:

也许,这对你有帮助

 user@host:/tmp$ sed -n '/select/,/^$/p;/delete/,/^$/p;/insert/,/^$/p;/update/,/^$/p' log.txt | sed -n '/^[0-9]/!H;//x;$x;s/\n\([^A]\)/ \1/gp' | awk -F'"' 'printf("%d.\t %s\n", NR, $4)'
1.   delete             from DMEXPLOG where CREATED < ? 
2.   select            * from DMEXPLOG where CREATED < ?

【讨论】:

【参考方案3】:

你当前的例子可以翻译成

sed -n '/statement being executed/ s/.*"//p; /Reason code/ s/".*//p'  log

【讨论】:

以上是关于如何从 db2 错误日志中 grep 下面的 sql 部分的主要内容,如果未能解决你的问题,请参考以下文章

选择子句上的 CASE 子句抛出 'SQ​​LCODE=-811, SQLSTATE=21000' 错误

两种方法可以查看db2错误代码信息

如何从安装在 DB2 中的 Java UDF 进行日志记录

IDEA 控制台输出日志无法grep

如何从 db2 表中删除数百万条记录

安卓测试查看日志输出