我想使用来匹配文件中以下模式的所有行grep
。
Abc::xyz() [18-Feb-15 12:09:16]
我想出了下面带有grep的正则表达式,但它没有返回匹配项。请帮助正确的正则表达式。
grep -E "^[:alnum:]::[:alnum:]\(\)[:space:]\[.*\]$" <file_name>
在python中,regex可以正常工作,但不能与grep一起使用。
^.*::.*\(\) \[.*\]$
如果要坚持使用,请使用以下方法:
grep -E "^[[:alpha:]]+::[[:alpha:]]+\(\)[[:space:]]\[.*\]$"
例如:
$ echo "Abc::xyz() [18-Feb-15 12:09:16]" | \
grep -E "^[[:alpha:]]+::[[:alpha:]]+\(\)[[:space:]]\[.*\]$"
输出:
Abc::xyz() [18-Feb-15 12:09:16]
这可以变得更简单:
grep -E "^[^:]+::[^(]+\(\) \[[^]]+\]$"
查看:
$ echo "Abc::xyz() [18-Feb-15 12:09:16]" | grep -E "^[^:]+::[^(]+\(\) \[[^]]+\]$"
Abc::xyz() [18-Feb-15 12:09:16]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句