【问题标题】:PCRE regular expression to match text except word groupPCRE正则表达式匹配除词组以外的文本
【发布时间】:2012-03-07 14:00:33
【问题描述】:

我正在努力调整我的正则表达式以过滤掉

  • rlwrap
  • 狂欢

来自:

cat file  
user   30300 29384  0 Mar05 pts/4    00:00:00 /opt/bin/rlwrap /opt/test/l64/app apps/bin -gw :1234 -p 10006  
user   30301 30300  0 Mar05 pts/5    00:00:00 /opt/test/l64/app apps/bin -gw :1234 -p 10006  
user   30300 29384  0 Mar05 pts/4    00:00:00 /opt/bin/bash /opt/test/l64/app apps/bin -gw :1234 -p 10006 

所以我只需要得到第二行:

grep -P 'regex' file  
user   30301 30300  0 Mar05 pts/5    00:00:00 /opt/test/l64/app apps/bin -gw :1234 -p 10006  

我拥有的是:

grep -P '((?:(?!rlwrap\b|bash\b).)*?)-p 10006$' file

但它不起作用:(

我只需要 PCRE 正则表达式,我需要让它与这个 grep -P 语句一起工作。
所以请不要使用 awk、perl、sed、grep -v 或类似的东西。

编辑
我真的很想知道为什么有人对这个问题投了反对票?

【问题讨论】:

  • 为什么不能用grep 进行多次传递,在以后的调用中使用-v
  • 因为将使用此正则表达式的应用程序不是调用其他应用程序并将 PCRE 正则表达式作为正则表达式进行匹配的应用程序

标签: regex pcre


【解决方案1】:

这将找到所有不包含/rlwrap/bash 后跟空格的行:

grep -P '^(?!.*/(rlwrap|bash) )' file

【讨论】:

  • 完美! '^' 完成了我最终得到的技巧:'^(?!.*/(rlwrap\b|bash\b)).*-p 10006$'
猜你喜欢
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 2011-02-04
相关资源
最近更新 更多