comment grep la ligne d’erreur et l’erreur provoquée par la ligne

J’ai besoin de grep le fichier error.log pour obtenir le *ERROR* et Caused by: pour la même balise d’erreur dans les lignes suivantes

Par exemple: j’ai le error.log comme le suivant

 08.02.2016 00:11:21.541 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp org.apache.sling.api.scripting.ScriptEvaluationException: at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388) at org.apache.sling.scripting.jsp.taglib.IncludeTagHandler.dispatch(IncludeTagHandler.java:59) at org.apache.sling.scripting.jsp.taglib.AbstractDispatcherTagHandler.doEndTag(AbstractDispatcherTagHandler.java:129) at org.apache.jsp.libs.foundation.components.parsys.parsys_jsp._jspService(parsys_jsp.java:318) at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502) ... 180 common frames omitted Caused by: org.apache.sling.api.SlingException: javax.servlet.ServletException: java.lang.Exception: user person search is not available 08.02.2016 05:28:56.748 *ERROR* [158.151.84.34 [1454927336516] GET /content/user/home/people-search.html HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502) ... 20 common frames omitted Caused by: java.lang.Exception: user person search is not available 08.02.2016 06:11:24.725 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp org.apache.sling.api.scripting.ScriptEvaluationException: at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388) ... 10 common frames omitted Caused by: org.apache.sling.api.SlingException: javax.servlet.ServletException: java.lang.Exception: user person search is not available 

J’ai besoin d’obtenir la sortie comme

 08.02.2016 00:11:21.541 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp Caused by: org.apache.sling.api.SlingException: javax.servlet.ServletException: java.lang.Exception: user person search is not available 08.02.2016 05:28:56.748 *ERROR* [158.151.84.34 [1454927336516] GET /content/user/home/people-search.html HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException Caused by: java.lang.Exception: user person search is not available 08.02.2016 06:11:24.725 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp Caused by: org.apache.sling.api.SlingException: javax.servlet.ServletException: java.lang.Exception: user person search is not available 

Comment vais-je l’obtenir?

Que diriez-vous:

 grep -e "\*ERROR\*" -e "Caused by:" error.log 
 egrep '\*ERROR\*|Caused by:' error.log 

Pouvez-vous avoir une ligne “Caused by:” qui n’a pas été précédée par une ligne “ERROR”? Par exemple, pourriez-vous obtenir un “AVERTISSEMENT” (ou similaire) suivi d’un “Causé par”? Qu’en est-il d’un “ERROR” qui n’est pas suivi d’un “Causé par:”? Qu’en est-il du texte dans une autre partie de la ligne que dans le ciblage (3ème champ pour ERREUR, début de la ligne pour Causé par)? Si cela se produit, modifiez votre question pour inclure ces cas dans votre exemple d’entrée / sortie.

C’est ce que vous devez être à l’abri de tous les problèmes ci-dessus:

 $ cat tst.awk /^[0-9]/ { if (inErr) { print "No cause found" } if ($3 == "*ERROR*") { print inErr = 1 } } inErr && /^Caused by:/ { print inErr = 0 } END { if (inErr) { print "No cause found" } } $ awk -f tst.awk file 08.02.2016 00:11:21.541 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp Caused by: org.apache.sling.api.SlingException: javax.servlet.ServletException: java.lang.Exception: user person search is not available 08.02.2016 05:28:56.748 *ERROR* [158.151.84.34 [1454927336516] GET /content/user/home/people-search.html HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException Caused by: java.lang.Exception: user person search is not available 08.02.2016 06:11:24.725 *ERROR* [158.151.84.34 [1454908281309] GET /content/user/home/people-search.html HTTP/1.1] com.day.cq.wcm.tags.IncludeTag Error while executing script content.jsp Caused by: org.apache.sling.api.SlingException: javax.servlet.ServletException: java.lang.Exception: user person search is not available