Shell 将2个工作GREP表达式合并为一个

Shell 将2个工作GREP表达式合并为一个,shell,Shell,我有一个包含以下日志项的文件: 格式1: INFO 07 May 2015 15:24:35,146 vert.x-worker-thread-19:40422-6 [28782710226944/personWebApiMultiPass:UnfilteredNamedEntityPipelineProcessor:unfilteredNamedEntityPipelineProcessor] [oiq.contentdigestion.PipelineProcessor] - COMPLE

我有一个包含以下日志项的文件:

格式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
注意:两行之间的差异是方括号内的时间格式
[]

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

grep -E "\[[0-9]* ms\]" filename
grep -E "\[[0-9]*ms\]" filename
为了匹配格式2行,我使用了以下grep命令:

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

有人可以帮助grep表达式匹配两个格式行吗?

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

grep -E '\[[0-9]+ ?ms\]' file
使用ERE时,
+
字符表示一个或多个在此上下文中更有意义的字符

或者,在基本模式下:

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

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

@user2862093,你应该像我在回答中所做的那样,避开外部的
\[
\]
,否则正则表达式的含义就会改变。