将2个有效的GREP表达式合并为一个

时间:2015-05-11 08:32:34

标签: shell

我有一个包含日志条目的文件,例如:

格式1:

INFO  07 May 2015 15:24:35,146 vert.x-worker-thread-19:40422-6 [28782710226944/personWebApiMultiPass:UnfilteredNamedEntityPipelineProcessor:unfilteredNamedEntityPipelineProcessor] [oiq.contentdigestion.PipelineProcessor] - COMPLETE >10000ms [16992ms]: http://example1.com/long-url/etc.html

格式2:

INFO  07 May 2015 15:24:34,648 vert.x-worker-thread-12:40464-2 [28782710226945/personWebApiMultiPass:HighlyAssociatedEntitiesPipelineProcessorInternal] [oiq.contentdigestion.PipelineProcessor] - COMPLETE [0 ms]: http://example2.com/yet-another-long-url/etc.html

注意:2行之间的差异是方括号[]中的时间格式。

为了匹配格式1行,我使用了以下grep命令:

grep -E "\[[0-9]* ms\]" filename

为了匹配格式2行,我使用了以下grep命令:

grep -E "\[[0-9]*ms\]" filename

任何人都可以帮助grep表达式匹配两种格式行吗?

1 个答案:

答案 0 :(得分:0)

您需要将空间设为可选。您可以使用?字符执行此操作,这意味着在扩展正则表达式模式下匹配“零或一个”:

grep -E '\[[0-9]+ ?ms\]' file

使用ERE,+字符表示一个或多个,在这种情况下更有意义。

或者,在基本模式下:

grep '\[[0-9]\{1,\} *ms\]' file

在这里,我使用\{1,\}替代了+,它不符合POSIX标准(尽管您可以将\+与GNU grep一起使用)。我还使用了*零或更多)来匹配可选空间。我想你不关心你的案例中是否有零个,一个或多个空格。如果您想要严格,可以将其更改为\{0,1\}

相关问题