正则表达式匹配一个而不是另一个

时间:2017-06-14 15:34:25

标签: regex bash

所以我有这个日志文件

2017-06-14 08:53:41,472 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:41,472 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:41,472 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:45,010 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-05-23 18:38:35,966 - [info] access - example.com "GET HTTP/1.1" 52228 200 0 159 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:37,811 - [info] access - example.com "GET HTTP/1.1" 53716 200 0 301 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-06-14 08:53:45,010 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:45,011 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:45,011 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 09:40:04,506 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 58.0 
2017-06-14 12:32:40,779 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0
2017-06-14 13:22:40,988 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:24:45,023 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:25:16,091 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:26:18,138 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-05-23 18:38:34,874 - [info] access - example.com "GET HTTP/1.1" 673 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:35,428 - [info] access - example.com "GET HTTP/1.1" 662 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:57,901 - [info] access - example.com "GET HTTP/1.1" 745 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-06-14 13:27:20,425 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:28:22,237 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:28:53,225 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:29:24,139 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:32:30,227 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-05-23 18:38:33,881 - [info] access - example.com "GET HTTP/1.1" 603 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:34,111 - [info] access - example.com "GET HTTP/1.1" 675 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:34,144 - [info] access - example.com "GET HTTP/1.1" 481 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-06-14 13:33:01,250 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:35:05,346 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:36:07,563 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:39:13,557 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:39:33,777 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 58.0
2017-05-23 18:38:58,445 - [info] access - example.com "GET HTTP/1.1" 859 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:58,485 - [info] access - example.com "GET HTTP/1.1" 819 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:58,720 - [info] access - example.com "GET HTTP/1.1" 654 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:58,760 - [info] access - example.com "GET HTTP/1.1" 624 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-06-14 13:39:44,517 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:45:25,661 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:46:58,629 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:49:02,639 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:49:40,884 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-05-23 18:38:34,369 - [info] access - example.com "GET HTTP/1.1" 683 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:58,181 - [info] access - example.com "GET HTTP/1.1" 886 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-05-23 18:38:58,210 - [info] access - example.com "GET HTTP/1.1" 662 304 0 0 "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
2017-06-14 13:50:04,713 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:50:27,767 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:50:27,767 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 13:50:27,767 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Internet Explorer 11.0
2017-06-14 14:04:44,239 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: PhantomJS 2.1

我想用一个正则表达式查找文件中的所有错误,但不是包含" Internet Explorer 11"的错误行。

使用regex101.com我能够找出这个正则表达式我能够找到一个与此匹配的正则表达式:(\[.*error.*\])(?!.*Internet Explorer 11.*)

regex101

现在我想知道如何在命令行上执行此操作来测试它。

我尝试了什么

这些显示没有输出(必须转义!

grep "(\[.*error.*\]){1,}(?\!.*Internet Explorer 11.*)" /var/log/upstart/app.log

egrep "(\[.*error.*\]){1,}(?\!.*Internet Explorer 11.*)" /var/log/upstart/app.log

任何人都知道如何做到这一点?任何帮助表示赞赏

3 个答案:

答案 0 :(得分:1)

(?!...)这样的先行模式特定于Perl正则表达式,而不是grep使用的常用基本和扩展正则表达式的一部分。

但是你想要的是用grep和管道很容易做到的事情:

grep -F '[error]' /var/log/upstart/app.log | grep -vF 'Internet Explorer 11'

或者您可以使用grep -P(使用GNU grep)或pcregrep,它们支持Perl正则表达式:

pcregrep '(\[.*error.*\])(?!.*Internet Explorer 11.*)' /var/log/upstart/app.log

(GNU grep的手册提到-P是"高度实验性的#34;但它并不是一个新功能,并且在实践中似乎工作精细。)

答案 1 :(得分:0)

使用单个 awk 命令:

awk '$4~/error/ && $0!~/Internet Explorer 11/' file

输出:

2017-06-14 08:53:41,472 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:41,472 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:41,472 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:45,010 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:45,010 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:45,011 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 08:53:45,011 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0 
2017-06-14 09:40:04,506 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 58.0 
2017-06-14 12:32:40,779 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 57.0
2017-06-14 13:39:33,777 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: Chrome 58.0
2017-06-14 14:04:44,239 - [error] javascript - "Javascript error: undefined - url: undefined - line: undefined - browser: PhantomJS 2.1

答案 2 :(得分:0)

有更简单的管道方法,不包括使用grep(-v)开关......但如果你确实需要纯正则表达式解决方案:

grep -P '^((?!Internet\ Explorer\ 11).)*\[error\]((?!Internet\ Explorer\ 11).)*$' filename

这里-P使用perl正则表达式,我认为这是前瞻性所必需的