我对 Linux Bash 脚本编写还很陌生,我现在正在做的是在日志文件中查找错误。我怎样才能只返回每个日志的时间而不是显示它的所有详细信息?以下是我的代码:
grep -n "Servlet.service() for servlet default threw exception" error
日志文件的结果之一:
337:17:44:59,136 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/mobilelife].[default]] (http-/0.0.0.0:28080-32) JBWEB000236: Servlet.service() for servlet default threw exception:java.lang.reflect.UndeclaredThrowableException
而我想要的结果只是时间,17:44:59,有人可以帮忙吗?
如果您要搜索日志文件中的特定文本,例如:Servlet.service() for servlet default throw exception
,文件中有许多其他条目,您将需要消除打印除包含搜索字符串的行之外的所有其他行,然后仅从该行输出相关时间。你可以用类似的东西来做到这一点:
sed -n '/Servlet.service() for servlet default threw exception/s/^[^:]*:\([^,]*\).*$/\1/p' log.file
基本上:
示例日志文件内容
$ cat log.file
37:17:44:59,136 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/mobilelife].[default]] (http-/0.0.0.0:28080-32) JBWEB000236: Servlet.service() for servlet default threw exception:java.lang.reflect.UndeclaredThrowableException
37:17:45:00,136 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/mobilelife].[default]] (http-/0.0.0.0:28080-32) JBWEB000236: Servlet.service1() for servlet default threw exception:java.lang.reflect.UndeclaredThrowableException
37:17:45:01,136 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/mobilelife].[default]] (http-/0.0.0.0:28080-32) JBWEB000236: Servlet.service2() for servlet default threw exception:java.lang.reflect.UndeclaredThrowableException
sed 使用/输出
$ sed -n '/Servlet.service() for servlet default threw exception/s/^[^:]*:\([^,]*\).*$/\1/p' log.file
17:44:59