egrep的模式

时间:2016-08-10 23:38:37

标签: regex grep

我有一个巨大的日志文件,其中包括运行时间。那是我要提取的信息。日志的行如下所示:

Info: Executed check 'data_existence', result 'pass', took 0 s.
Info: Executed check 'message', result 'pass', took 20 s.
Info: Executed check 'blu', result 'pass', took 2 minutes.
Info: Executed check 'bla', result 'pass', took 2.5 minutes.
Info: Executed check 'foo', result 'pass', took 3.4 hours.
Info: Executed check 'bar', result 'pass', took 2.7 days.

我想提取所有说'&n;信息...采取' (中间还有大量的其他内容)但是为了减少混乱,我想跳过仅涉及秒的行。

所以我写道:

egrep 'Info: .*took\s*\d*\s*[mhd]' LOGs/my.log

令人惊讶的是(对我来说)它不起作用(它回来了空白)。虽然https://regex101.com/的检查员说我的模式找到了一些东西。

缺少什么?

谢谢, 格特

@ John1024

sc-xterm-26:~> cat test
Info: Executed check 'data_existence', result 'pass', took 0 s.
Info: Executed check 'message', result 'pass', took 20 s.
Info: Executed check 'blu', result 'pass', took 2 minutes.
Info: Executed check 'blu', result 'pass', took 12 minutes.
Info: Executed check 'bla', result 'pass', took 2.5 minutes.
Info: Executed check 'foo', result 'pass', took 3.4 hours.
Info: Executed check 'bar', result 'pass', took 2.7 days.
sc-xterm-26:~>
sc-xterm-26:~>
sc-xterm-26:~> uname -a
Linux sc-xterm-26 3.0.52 #2 SMP Thu Dec 6 02:40:34 PST 2012 x86_64 x86_64 x86_64 GNU/Linux
sc-xterm-26:~> grep --version
grep (GNU grep) 2.5.1

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR     PURPOSE.

sc-xterm-26:~> grep -E 'Info: .*took\s*[0-9.]*\s*[mhd]' test
sc-xterm-26:~>
sc-xterm-26:~> grep -E 'Info: .*took\s*[[:digit:].]*\s*[mhd]' test
sc-xterm-26:~>

@All

我把查询放到TCL脚本中,它运行正常。不再需要基于grep的解决方案。最好的,格特。

1 个答案:

答案 0 :(得分:1)

grep无法识别\d。尝试:

$ grep -E 'Info:.*took\s*[0-9.]*\s*[mhd]' logfile
Info: Executed check 'bla', result 'pass', took 2.5 minutes.
Info: Executed check 'foo', result 'pass', took 3.4 hours.
Info: Executed check 'bar', result 'pass', took 2.7 days.

或者,更好的是:

$ grep -E 'Info:.*took\s*[[:digit:].]*\s*[mhd]' logfile
Info: Executed check 'bla', result 'pass', took 2.5 minutes.
Info: Executed check 'foo', result 'pass', took 3.4 hours.
Info: Executed check 'bar', result 'pass', took 2.7 days.

注意:

  1. egrep已弃用。请改用grep -E

  2. grep应该支持POSIX正则表达式。 \s是GNU扩展,可能无法移植。

  3. 不支持\d
  4. [:digit:]是unicode-safe,这使它成为比0-9更好的选择。

  5. 要匹配浮点数,除数字外还必须允许小数点。请注意,在[...]之外,句点.是通配符。相比之下,在[...]内,它只匹配一个句号。

  6. 更便携版

    对于不支持\s的greps,请尝试:

    $ grep -E 'Info:.*took[[:space:]]*[[:digit:].]*[[:space:]]*[mhd]' logfile
    Info: Executed check 'bla', result 'pass', took 2.5 minutes.
    Info: Executed check 'foo', result 'pass', took 3.4 hours.
    Info: Executed check 'bar', result 'pass', took 2.7 days.