2016-07-31 32 views
-1

我有一個腳本,它從db2diag.log中提取錯誤消息。我必須從下面的文件中提取導致死鎖的SQL查詢。如何從db2錯誤日誌grep以下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)

+2

翻譯請看看[編輯的幫助(http://stackoverflow.com/editing-幫幫我)。 – Cyrus

回答

0
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是空的,因爲你給出的數據有實際數據之間的空行。實際數據中是否有空行?

0

也許,這可以幫助您

[email protected]:/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 < ? 
0

您當前的例子可以用

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