grep は最後の N 行と一致します。

grep は最後の N 行と一致します。

特定の一致後に20行以上をgrepするコマンドを探しています。

例: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)

ベストアンサー1

grepの-Anスイッチを使用して一致後にn行を取得することができます。たとえば、次のようになります。

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

編集:あなたのgrepバージョンが-Aをサポートしていないようです。ここで使用できる小さなスクリプトがあります。

#!/bin/sh
file="$1"
pattern="$2"
n="$3"
matches="$(grep -n ${pattern} ${file})"
echo "${matches}" | while read match;  do
    line="${match%%:*}"
    sed -n "${line},$((${line} + ${n} - 1))p" < "${file}"
done

次のように使用できます。sh script.sh mylog_file.log "09:55:21,651" 20

編集2:私がここにいる間にsedが奇妙な場合に備えて、sedの代わりにheadとtailを使用するソリューションがあります。

#!/bin/sh
file="$1"
pattern="$2"
n="$3"
matches="$(grep -n ${pattern} ${file})"
echo "${matches}" | while read match;  do
    line="${match%%:*}"
    head "-$((${line} + ${n} - 1))" "${file}" | tail "-${n}"
done

おすすめ記事