当前位置: 技术问答>linux和unix
关于grep的一个问题,请指点
来源: 互联网 发布时间:2017-03-06
本文导语: 我想用linux的grep命令做个日志抓取程序: 要求就是抓取带有'exception'的关键字,并且连同带有该关键字的后续多行详细报错也一起抓出来,日志如下(就是当检索到带有'exception'关键字的行,需要将该关键字一下...
我想用linux的grep命令做个日志抓取程序:
要求就是抓取带有'exception'的关键字,并且连同带有该关键字的后续多行详细报错也一起抓出来,日志如下(就是当检索到带有'exception'关键字的行,需要将该关键字一下的,带有'at'开头的后续几行也一起抓出来,没有‘at’开头的行时,则继续抓取下一个'exception'关键字)
avax.servlet.ServletException: Connection reset
at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at com.neusoft.unieap.action.EAPActionServlet.process(EAPActionServlet.java:783)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service$WTImpl$javax$servlet$http$HttpServlet(HttpServlet.java:856)
======menuName=ReportExport
menuId=Export,menuName=ReportExport
-----ECSide---now bufferView is true
-----ECSide---now bufferView is true
======menuName=ReportExport
menuId=Export,menuName=ReportExport
-----ECSide---now bufferView is true
-----ECSide---now bufferView is true
要求就是抓取带有'exception'的关键字,并且连同带有该关键字的后续多行详细报错也一起抓出来,日志如下(就是当检索到带有'exception'关键字的行,需要将该关键字一下的,带有'at'开头的后续几行也一起抓出来,没有‘at’开头的行时,则继续抓取下一个'exception'关键字)
avax.servlet.ServletException: Connection reset
at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at com.neusoft.unieap.action.EAPActionServlet.process(EAPActionServlet.java:783)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service$WTImpl$javax$servlet$http$HttpServlet(HttpServlet.java:856)
======menuName=ReportExport
menuId=Export,menuName=ReportExport
-----ECSide---now bufferView is true
-----ECSide---now bufferView is true
======menuName=ReportExport
menuId=Export,menuName=ReportExport
-----ECSide---now bufferView is true
-----ECSide---now bufferView is true
|
为了除去最后一行,再过滤一次吧。
$ sed -n '/上午08时.*Exception/{p;n;:loop;n;s/^at /at /;p;t loop}' log.txt |sed '$d'