Linux 如何根据错误关键字从日志文件中提取行?

Linux 如何根据错误关键字从日志文件中提取行?,linux,bash,shell,scripting,Linux,Bash,Shell,Scripting,这是一个示例日志文件(如下)。当关键字ORA-02063存在于日志中的任何一行中时,我想提取日志文件中具有相同时间戳的所有行 20200814 13:54:34 DEBUG (com.test.test.test.test2Servlet) [doPost] Incoming XML request: <?xml version="1.0" encoding="UTF-8"?> <webdoc> </contract>

这是一个示例日志文件(如下)。当关键字ORA-02063存在于日志中的任何一行中时,我想提取日志文件中具有相同时间戳的所有行

20200814 13:54:34 DEBUG (com.test.test.test.test2Servlet) [doPost]  Incoming XML request: <?xml version="1.0" encoding="UTF-8"?>
<webdoc>
</contract>
<notes>


some data

</notes>
    </contract>
</webdoc> 
20200814 13:54:34 ERROR (com.test.test.test.test2Servlet) [doPost] request failed: ccom.test.test.test.test2Servlet.exception.SystemFailureException: Database operation failed  - caused by: java.sql.BatchUpdateException: ORA-01461: can bind a LONG value only for insert into a LONG column
ORA-02063: preceding stringstring from string```



我曾尝试使用grep,但由于无法提取时间戳,因此无法使用grep

20200814 13:54:34 DEBUG (com.test.test.test.test2Servlet) [doPost]  Incoming XML request: <?xml version="1.0" encoding="UTF-8"?>
<webdoc>
</contract>
<notes>


some data

</notes>
    </contract>
</webdoc> 
20200814 13:54:34 ERROR (com.test.test.test.test2Servlet) [doPost] request failed: ccom.test.test.test.test2Servlet.exception.SystemFailureException: Database operation failed  - caused by: java.sql.BatchUpdateException: ORA-01461: can bind a LONG value only for insert into a LONG column
ORA-02063: preceding stringstring from string```



20200814 13:54:34调试(com.test.test.test.test2Servlet)[doPost]传入的XML请求:
一些数据
20200814 13:54:34错误(com.test.test.test.test2Servlet)[doPost]请求失败:ccom.test.test.test.test2Servlet.exception.SystemFailureException:数据库操作失败-原因:java.sql.BatchUpdateException:ORA-01461:只能为插入到长列绑定长值
ORA-02063:字符串的前一个字符串```

您可以尝试这种方法,但不是最好的,而是一种解决方案

grep "`grep 'ORA-02063' logfile -B1 | cut -d ' ' -f1,2 | head -n 1`" logfile

使用文件webdoc中的示例数据尝试此操作:

dayte=$(awk -v str="ORA-01461" '{ if (match($0,str)) { print substr($0,0,17) } }' webdoc)
使用awk传递要搜索的字符串,如果该字符串存在于行中,则使用substr函数获取日期和时间戳并打印。将结果读入变量dayte

设置变量dayte后,使用sed打印所需的行:

sed -n '/'"$dayte"'/,/^<\/webdoc>/p' webdoc
sed-n'/'“$dayte”/,/^/p”webdoc
将包含相关日期的所有行打印到结束webdoc标记


注意-确保在sed命令中引用dayte变量。

对于问题中的日志文件片段,
ORA-02063
显示在没有日期/时间戳的行上;这是正确的(错误/数字可能出现在没有日期/时间戳的行上)还是这是一个输入错误,并且
ORA-02063
应该是前一行的一部分?@markp fuso这不是一个输入错误,这些行被记录了下来,看看你的问题历史,看起来过去有几个人帮过你,但不清楚他们的答案是否有任何好处;你可能想回顾一下马尔普·福索是的,随便什么。。。是的,不管怎样;取消回答;祝你好运