正则表达式从日志中排除某些错误类型

时间:2016-11-13 12:32:53

标签: regex regex-negation

我对此比较陌生,但我需要帮助来编写一个正则表达式来从日志中排除某些已知的错误类型。

00:11:04 [0] 70-Error: Invalid index command: "/search.asp".
00:11:04 [0] 70-Error: Invalid index command: "/wingate-internal//boot.ini".
00:11:04 [0] 70-Error: Invalid index command: "/".

并排除这个:

04:16:46 [8] 70-Error: Action failed - unencrypted communication is not  allowed (10.40.88.11): "ACTION=GETSTATUS".
04:14:17 [7] 70-Error: Action failed - unencrypted communication is not allowed (10.40.88.11): "ACTION=GETSTATUS".

我在同一个日志中有其他错误类型,可以用于报告,例如:

17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d

换句话说,我希望正则表达式报告除上述两种类型之外的所有错误。

我尝试创建这个正则表达式,但它似乎不起作用:

/(?:)(?:[^Error\:\ Action\ failed\ \-\ unencrypted\ communication\ is\ not\ allowed]*)(?:[^Error\:\ Invalid\ index\ command\:]*)/m

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

怎么样:

^(?!.*Error: Invalid index command)(?!.*Error: Action failed - unencrypted communication is not allowed)

<强>解释

^                           : begining of the string
(?!                         : negative lookahead (asserts that the following is not present in the string
    .*                      : 0 or more (*) any character but newline (.)
    Error: Invalid index command   : literally
)                           : end of lookahead
(?!                         : negative lookahead (asserts that the following is not present in the string
    .*                      : 0 or more (*) any character but newline (.)
    Error: Action failed - unencrypted communication is not allowed : literally
)                           : end of lookahead

此正则表达式匹配所有不包含Error: .........

的行

在perl脚本中使用:

#!/usr/bin/perl
use Modern::Perl;

my $re1 = qr/^(?!.*Error: Invalid index command)(?!.*Error: Action failed - unencrypted communication is not allowed)/;

while(<DATA>) {
    print if /$re1/;
}

__DATA__
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
00:11:04 [0] 70-Error: Invalid index command: "/search.asp".
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
00:11:04 [0] 70-Error: Invalid index command: "/wingate-internal//boot.ini".
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
00:11:04 [0] 70-Error: Invalid index command: "/".
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
04:16:46 [8] 70-Error: Action failed - unencrypted communication is not allowed (10.40.88.11): "ACTION=GETSTATUS".
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
04:14:17 [7] 70-Error: Action failed - unencrypted communication is not allowed (10.40.88.11): "ACTION=GETSTATUS".
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d

<强>输出:

17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d
17:43:17.370 ExecuterW: 957:Error [2400] DB Matters - addDocToWorklist - doSqlCommand: Error executing SQL statement - CID ed83d1e0d

答案 1 :(得分:0)

试试这个正则表达式:

\d+:\d+:\d+\s+\[\d\]\s+\d+-(Error:\s+Action\s+failed\s+\-\s+unencrypted\s+communication\s+is\s+not\s+allowed\s+.+|Error:\s+Invalid\s+index\s+command:\s+.+)

已对多个输入进行了here测试。

对它正在做什么的一点介绍和解释:

\ d:表示匹配个别数字。对于数字23,这将匹配2和3作为单独的数字但不作为一个数字 \ d +:表示匹配至少一个数字后跟任意数量的多个数字。它将23读作一个数字。 \ s:表示匹配一个空格 \ s +:匹配多个空格甚至是一个标签。有些人可能会说我们可以使用\ t作为选项卡,但是tab是空格的连接,所以在这里使用了它。 []:表示匹配方括号内的任何一个字符 ():表示匹配小括号内的所有表达式

<强>说明: 你的输入字符串:

  

04:16:46 [8] 70-错误:操作失败 - 未加密的通信是   不允许(10.40.88.11):&#34;行动= GETSTATUS&#34;。

-->`\d+:\d+:\d+\s+\[\d\]` 
  

匹配04:16:46 [8]

-->\s+\[\d\]\s+\d+- 
  

匹配[8] 70-包括[8]之前的空格

-->(Error:\s+Action\s+failed\s+\-\s+unencrypted\s+communication\s+is\s+not\s+allowed\s+.+ 
  

匹配直到错误:操作失败 - 未加密的通信不是   允许(10.40.88.11):&#34;行动= GETSTATUS&#34;。

请注意上面的正则表达式.+在结尾意味着,在允许空格之后匹配其他所有内容。 希望这澄清。

|代表OR

-->Error:\s+Invalid\s+index\s+command:\s+.+) 
  

匹配错误:索引命令无效:后跟任何   字符串/数字/特殊字符

注意:(a | b)匹配表达式a和b(如果两者都存在),否则它与存在的表达式匹配。