问题描述
这是一个示例日志文件(如下)。当日志中的任何行中都存在关键字ORA-02063时,我要提取具有相同时间戳的日志文件中的所有行。
我尝试使用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```
解决方法
您可以尝试这种方法,不是最好的方法,而是一种解决方法
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,17) } }' webdoc)
使用awk传递要搜索的字符串(如果该字符串存在于行中),请使用substr函数获取日期和时间戳并打印。将结果读入可变Dayte。
设置了变量dayte后,使用sed打印所需的行:
sed -n '/'"$dayte"'/,/^<\/webdoc>/p' webdoc
将所有包含相关日期的行打印到结束的webdoc标记中。
注意-确保sed命令中引用了dayte变量。