I am looking for a command where I can grep 20 or more lines after a specific match.

Example: grep "09:55:21,651" mylog_file.log

2018-02-26 09:55:21,651 ERROR  [WebContainer : 0] (CommonAction.java:253) - SITAConnector Error: Empty SITA Response XML
com.ac.ccaswitch.exception.SitaConnectorException: Empty SITA Response XML
        at com.ac.ccaswitch.connector.sita.SITAConnector.sendToSitaQueue(SITAConnector.java:144)
        at com.ac.ccaswitch.entry.CommonAction.performTask(CommonAction.java:212)
        at com.ac.ccaswitch.entry.PaymentServlet.doPost(PaymentServlet.java:85)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1694)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:970)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:508)
        at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)
        at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
        at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:878)
        at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
        at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:191)
        at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:454)
        at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:516)
        at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:307)
        at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:84)
        at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
        at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
        at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
        at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
        at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
        at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)

you can use grep's -An switch to get n lines after the match so for your example that would be

grep -A20 "09:55:21,651" mylog_file.log

EDIT: It looks like your version of grep does not support -A. So here is a small script you can use instead

matches="$(grep -n ${pattern} ${file})"
echo "${matches}" | while read match;  do
    sed -n "${line},$((${line} + ${n} - 1))p" < "${file}"

you can use it like this: sh script.sh mylog_file.log "09:55:21,651" 20

EDIT2: While I am at it here is a solution that uses head and tail instead of sed just in case your sed is weird aswell

matches="$(grep -n ${pattern} ${file})"
echo "${matches}" | while read match;  do
    head "-$((${line} + ${n} - 1))" "${file}" | tail "-${n}"
| improve this answer | |
  • This is not working. I am getting this help content - abbcd@acsayul000012:/opt/logs$ grep -A 20 "09:55:21,651" CCASWITCH.log grep: illegal option -- A usage: grep [-r] [-R] [-H] [-L] [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] -e pattern_list... [-f pattern_file...] [file...] usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] [-e pattern_list...] -f pattern_file... [file...] usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] pattern_list [file...] abbcd@acsayul000012:/opt/logs$ – Madhuraj Feb 27 '18 at 4:30
  • what version of grep are you using? – Captain Wobbles Feb 27 '18 at 4:35
  • How to check the version of grep? – Madhuraj Feb 27 '18 at 4:45
  • grep --version or grep -V – Captain Wobbles Feb 27 '18 at 5:04
  • 1
    ok from the error message i am guessing that you are running a solaris system. You can either install gnu grep which does support -A or try the script i edited in – Captain Wobbles Feb 27 '18 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.