shell 脚本上的 grep 命令

shell 脚本上的 grep 命令

我有一个日志文件,我正在尝试编写一个 shell 脚本来查找文件中多行的多个字符串。

例如,如果我在日志中看到短语"class=com.comcast.parker.GetCurrentAudioLanguageResponse"和,我应该打印else 。所以我的 shell 脚本是这样的:"status:OK"getAudioLanguage SUCCESSFULgetAudioLanguage FAILED

logfile=$1

if grep "{\"class\":\"com.comcast.parker.GetCurrentAudioLanguageResponse\"" jags.txt | grep "{\"status\":\"OK\"" | wc -l ; then
    echo "getAudioLanguage SUCCESSFUL"
fi
if grep "{\"class\":\"com.comcast.parker.GetCurrentAudioLanguageResponse\"" $logfile | grep -q "GENERAL_ERROR" | wc -l ; then
    echo "getAudioLanguage FAILED"
fi

在日志中,我得到的状态为GENERAL_ERROR,因此它应该打印FAILED,但输出显示为getAudioLanguage SUCCESSFUL..

日志档案:

160125-11:11:28.442500 [mod=SYS, lvl=INFO] [tid=2332] ======= Message is onRPCCall ======>
160125-11:11:28.442614 [mod=SYS, lvl=INFO] [tid=2332] Entering onRPCCallEvent for request ---> getAudioLanguage
160125-11:11:28.442845 [mod=SYS, lvl=INFO] [tid=2332] Received json request = {"event":1,"handler":1,"name":"onRPCCall","params":{"callGUID":"d75a5bab-6b29-4ab4-9f7e-3cf32d4a05b1","callParams":[{"getAudioLanguage":{}}],"class":"com.comcast.xre.events.XRERPCCallInfo","destinationSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d","method":"getAudioLanguage","sourceSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d"},"phase":"STANDARD","source":1}

160125-11:11:28.442920 [mod=SYS, lvl=INFO] [tid=2332] GetCurrentAudioLanguage : Entered
160125-11:11:28.442968 [mod=SYS, lvl=INFO] [tid=2332] pMainPlayer is NULL.
160125-11:11:28.443012 [mod=SYS, lvl=INFO] [tid=2332] getAudioLanguage: Get Audio Language returned ��v`>       T�=     T0~�t
160125-11:11:28.443676 [mod=SYS, lvl=INFO] [tid=2332] ====== Response sending is {"appId":1,"command":"CALL","commandIndex":5,"method":"generateAppEvent","params":[{"class":"com.comcast.xre.events.XREOutgoingEvent","name":"onRPCReturn","params":{"callGUID":"d75a5bab-6b29-4ab4-9f7e-3cf32d4a05b1","class":"com.comcast.xre.events.XRERPCReturnInfo","destinationSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d","method":"getAudioLanguage","returnVal":{"class":"com.comcast.parker.GetCurrentAudioLanguageResponse","status":"GENERAL_ERROR","statusMessage":"Getting Current Audio Language was unsuccessful."},"sourceSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d"}},"ab00ebea-5f63-4619-9877-273a2bceea1d"],"targetId":1,"targetPath":"","timestamp":0}

答案1

AWK 可能更容易使用一些:

awk '/GetCurrentAudioLanguageResponse/ && /status\"\:\"GENERAL_ERROR/{ print "FAIL" } /GetCurrentAudioLanguageResponse/ && /status\":\"OK/ {print "SUCCESS"}  ' log.txt

在此代码中,我们匹配两个模式,如果模式匹配 - 我们运行大括号中的内容。对于GetCurrentAudioLanguageResponsestatus":"GENERAL_ERROR我们打印“失败”,对于模式GetCurrentAudioLanguageResponsestatus\":\"OK我们打印“成功”

答案2

您应该检查 grep 命令的返回状态。例如,如果我的文件包含 --

  a.b.c.d status:FAILED

运行命令

  [[ $(grep "a.b.c.d" log | grep "FAIL") ]] && echo  "FAILED"

 FAILED

答案3

我愿意给你提供使用sed解析日志文件:

sed -n '
/{"class":"com.comcast.parker.GetCurrentAudioLanguageResponse"/{
    /"GENERAL_ERROR"/{
        c\getAudioLanguage FAILED
        p
        q
    }
    /"status":"OK"/{
        c\getAudioLanguage SUCCESSFUL
        h
    }
}
${
g
/SUCCESSFUL/p
}
' logfile

相关内容