给出一个示例日志文件,如:
interesting stuff
App: 123 blob
some text
other text
ERROR could not find file XYZ
other really cool stuff
App: 123 blob
some text
other text
ERROR stdout blocked
我想用'错误'和'应用:'加上'应用:'之前的行来获取该行。 使用多个搜索模式和-B 1我得到匹配的行加上之前的相应行:
grep -B 1 -e '^App:.*' -e '.*ERROR.*'
interesting stuff
App: 123 blob
--
other text
ERROR could not find file XYZ
other really cool stuff
App: 123 blob
--
other text
ERROR stdout blocked
(我也不知道' - '来自哪里) 但我想要的输出是:
interesting stuff
App: 123 blob
ERROR could not find file XYZ
other really cool stuff
App: 123 blob
ERROR stdout blocked
非常感谢。
答案 0 :(得分:3)
grep
' -B 1
选项将适用于所有表达式。在这种情况下,您可以使用awk
:
$ awk '/App/ {print line;print} {line=$0} /ERROR/' file
interesting stuff
App: 123 blob
ERROR could not find file XYZ
other really cool stuff
App: 123 blob
ERROR stdout blocked
$