如何仅用括号grep文本

如何仅用括号grep文本,grep,Grep,日志如下所示: 2019/10/04 03:03:12.938 [error]: some exception happened 2019/10/04 03:03:12.938 [error]: some exception happened 2019/10/04 03:53:35.595 [info]: there is a benign error in the code grep“error”log.txt将提供所有三行,但我只需要前两行。如何做到这一点?尝试使用以下模式: ^.*\[e

日志如下所示:

2019/10/04 03:03:12.938 [error]: some exception happened
2019/10/04 03:03:12.938 [error]: some exception happened
2019/10/04 03:53:35.595 [info]: there is a benign error in the code

grep“error”log.txt将提供所有三行,但我只需要前两行。如何做到这一点?

尝试使用以下模式:

^.*\[error\]:.*$
例如:

grep "^.*\[error\]:.*$"

您可以将包含
[错误]:

grep '\[error]:' file
awk '$3 == "[error]:"' file
或者,您可以使用
awk
检查第三个字段是否等于
[错误]:

grep '\[error]:' file
awk '$3 == "[error]:"' file

#/bin/bash
log=“2019/10/04 03:03:12.938[错误]:发生了一些异常
2019/10/04 03:03:12.938[错误]:发生了一些异常
2019/10/04 03:53:35.595[信息]:代码中存在良性错误“

grep'\[error]:'感谢您的回答。但是,问题是日志包含[error]和[info]的颜色代码。在控制台中它显示为[error],而在文本中它实际上是[^[[31merror^[[0m]

关闭颜色代码的步骤

cat xxx.log | sed 's/\x1b\[[0-9;]*m//g'
要grep[错误]:

cat xxx.log | sed 's/\x1b\[[0-9;]*m//g' | grep "\[error\]"

grep“\[error\]”log.txt
你能试试吗?
^.*.
*$
没有任何用处。@EdMorton你和Ed Norton有亲戚关系吗?