在两种模式之间搜索和捕获多个组

时间:2017-10-22 05:30:47

标签: bash awk sed grep

我需要捕获警报日志的内容,以查找以ORA-01555开头的行及其相关的SQL语句。

文件内容如下:

Sat Oct 21 12:11:40 2017
Thread 1 advanced to log sequence 143 (LGWR switch)
  Current log# 5 seq# 143 mem# 0:
Sat Oct 21 12:12:22 2017
Thread 1 cannot allocate new log, sequence 144
Private strand flush not complete
  Current log# 5 seq# 143 mem# 0:
Sat Oct 21 12:12:22 2017
ORA-01555 caused by SQL statement below (SQL ID: 1sfgms7z66zrs, SCN: 0x0401.1e45bbc4):
Sat Oct 21 12:11:48 2017
CREATE INDEX AAD_SAA_ADB_RESULTS_AUD_GTI1 ON AAD_PS_SAA_ADB_RESULTS_AUD_GT (
 rpt_date
)
Sat Oct 21 12:20:56 2017
Thread 1 advanced to log sequence 153 (LGWR switch)
  Current log# 6 seq# 153 mem# 0: 
+REDO_VOL1/DSS1STG/ONLINELOG/group_6.262.957710915
  Current log# 6 seq# 153 mem# 1: 
+REDO_VOL1/DSS1STG/ONLINELOG/group_6.263.957710917
Sat Oct 21 12:21:11 2017
ORA-01555 caused by SQL statement below (SQL ID: 9ad5awvfvsfd9, Query 
Duration=6444 sec, SCN: 0x0401.1e53de65):
Sat Oct 21 12:21:11 2017
SELECT a.EMPLID,    a.TEST_ID,    a.TEST_COMPONENT,    a.SCORE
    FROM
    ODS_PSE.PS_STDNT_TEST_COMP a,
    DSS_RDS.IR_STU_TST_SCR_SAT_VLD_DT_ST b
    where
    a.TEST_COMPONENT in ('VE','MA')
    AND a.EMPLID = b.PRSN_UNIV_ID
    AND ROUND(a.SCORE) = a.SCORE
    AND a.TEST_ID = b.STU_TST_CD
    AND a.TEST_DT = b.TEST_DT
    AND a.LS_DATA_SOURCE = b.LS_DATA_SOURCE
UNION
    SELECT a.EMPLID,    a.TEST_ID,    a.TEST_COMPONENT,    a.SCORE
    FROM
    ODS_PSE.PS_STDNT_TEST_COMP a,
    DSS_RDS.IR_STU_TST_SCR_SAT_VLD_DT_ST b
    where
    a.TEST_COMPONENT in ('ERWS','MSS','MT','RT','WLT') 
    AND a.EMPLID = b.PRSN_UNIV_ID
    AND ( 
            (ROUND(a.SCORE) = a.SCORE and a.TEST_COMPONENT in 
('ERWS','MSS','RT','WLT'))
        or  (a.SCORE BETWEEN 10 AND 40  and a.TEST_COMPONENT in ('MT'))
        )
    AND a.TEST_ID = b.STU_TST_CD
    AND a.TEST_DT = b.TEST_DT
    AND a.LS_DATA_SOURCE = b.LS_DATA_SOURCE
Sat Oct 21 13:05:01 2017
Thread 1 advanced to log sequence 173 (LGWR switch)
  Current log# 5 seq# 173 mem# 0:     +REDO_VOL1/DSS1STG/ONLINELOG/group_5.257.957710909
  Current log# 5 seq# 173 mem# 1:     +REDO_VOL1/DSS1STG/ONLINELOG/group_5.256.957710911

我想要捕捉的内容是:

ORA-01555 caused by SQL statement below (SQL ID: 1sfgms7z66zrs, SCN: 0x0401.1e45bbc4):
Sat Oct 21 12:11:48 2017
CREATE INDEX AAD_SAA_ADB_RESULTS_AUD_GTI1 ON AAD_PS_SAA_ADB_RESULTS_AUD_GT (
 rpt_date
)
ORA-01555 caused by SQL statement below (SQL ID: 9ad5awvfvsfd9, Query 
Duration=6444 sec, SCN: 0x0401.1e53de65):
Sat Oct 21 12:21:11 2017
SELECT a.EMPLID,    a.TEST_ID,    a.TEST_COMPONENT,    a.SCORE
    FROM
    ODS_PSE.PS_STDNT_TEST_COMP a,
    DSS_RDS.IR_STU_TST_SCR_SAT_VLD_DT_ST b
    where
    a.TEST_COMPONENT in ('VE','MA')
    AND a.EMPLID = b.PRSN_UNIV_ID
    AND ROUND(a.SCORE) = a.SCORE
    AND a.TEST_ID = b.STU_TST_CD
    AND a.TEST_DT = b.TEST_DT
    AND a.LS_DATA_SOURCE = b.LS_DATA_SOURCE
UNION
    SELECT a.EMPLID,    a.TEST_ID,    a.TEST_COMPONENT,    a.SCORE
    FROM
    ODS_PSE.PS_STDNT_TEST_COMP a,
    DSS_RDS.IR_STU_TST_SCR_SAT_VLD_DT_ST b
    where
    a.TEST_COMPONENT in ('ERWS','MSS','MT','RT','WLT') 
    AND a.EMPLID = b.PRSN_UNIV_ID
    AND ( 
            (ROUND(a.SCORE) = a.SCORE and a.TEST_COMPONENT in 
('ERWS','MSS','RT','WLT'))
        or  (a.SCORE BETWEEN 10 AND 40  and a.TEST_COMPONENT in ('MT'))
        )
    AND a.TEST_ID = b.STU_TST_CD
    AND a.TEST_DT = b.TEST_DT
    AND a.LS_DATA_SOURCE = b.LS_DATA_SOURCE

这是我正在使用的grep,但正如您在结果中看到的那样,它不会捕获每个ORA-01555行及其整个SQL语句的内容。不确定为什么我的负面前瞻不太合适:

grep -Pzo 'ORA-01555.*\n(^.*[0-9]{2}:[0-9]{2}:[0-9]{2}.*)\n.*(?!.*[0-9]{2}:[0-9]{2}.*\n).+' alert.log

ORA-01555 caused by SQL statement below (SQL ID: 1sfgms7z66zrs, SCN: 0x0401.1e45bbc4):
Sat Oct 21 12:11:48 2017
CREATE INDEX AAD_SAA_ADB_RESULTS_AUD_GTI1 ON AAD_PS_SAA_ADB_RESULTS_AUD_GT (
ORA-01555 caused by SQL statement below (SQL ID: 9ad5awvfvsfd9, Query Duration=6444 sec, SCN: 0x0401.1e53de65):
Sat Oct 21 12:21:11 2017
SELECT a.EMPLID,    a.TEST_ID,    a.TEST_COMPONENT,    a.SCORE

2 个答案:

答案 0 :(得分:2)

您可以尝试使用此sed命令。

sed -E '/ORA-01555/!d;:A;N;/\n[A-Za-z]{3} [A-Za-z]{3} [0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}$/!bA;:B;N;/\n[A-Za-z]{3} [A-Za-z]{3} [0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}$/!bB;s/(.*)\n.*/\1/' infile

如果一行包含ORA-01555 - >保持在patern空间。

在patern空间中取下一行而不是日期。

将第一个日期保留在模式空间中。

继续接下一行,而不是约会。

删除patern空间中的最后一行,即日期。

打印模式空间并返回到开头,找到ORA-01555的新行。

答案 1 :(得分:0)

awk 解决方案:

awk '/ORA-01555/{ f=1 }f && /^[A-Z][a-z]{2} .* [0-9]{4}$/{ date++; if(date>=2) f=date=0 }f' alert.log