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

how to grep below sql part from db2 error log

我有一个脚本可以从 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)

awk '{gsub(/^.*The current statement being executed is \"|\". Reason code.*$/,""); print NR". "[=10=]}' log.txt
1. delete from DMEXPLOG where CREATED < ? 
2. 
3. select * from DMEXPLOG where CREATED < ?

毫无疑问,匹配的字符串可以更短,而 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]\)/ /gp' | awk -F'"' '{printf("%d.\t %s\n", NR, )}'
1.   delete             from DMEXPLOG where CREATED < ? 
2.   select            * from DMEXPLOG where CREATED < ?

您当前的示例可以翻译为

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