I have a log file and I am trying to write a shell script to find multiple strings in a file on multiple lines.

For example if I see the phrases "class=com.comcast.parker.GetCurrentAudioLanguageResponse" and "status:OK" together in the logs, I should print getAudioLanguage SUCCESSFUL else getAudioLanguage FAILED. So my shell script goes like this:


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

In the logs, I get status as GENERAL_ERROR, so it should print FAILED, but the output shows as 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}
  • I see you're trying to approach JSON by simple grepping. Well this may work, however instead I would recommend you to get familiar with jq tool. It can actually "understand" JSON. Small example : grep -Eo '(json request|Response sending is).*' a | cut -f 4- -d ' ' | jq '.params[].params.returnVal.status == "GENERAL_ERROR"' 2> /dev/null trueAs you can see it returns 'true' value which can later be easily used in some bash if loop.
    – MAQ
    Jan 28, 2016 at 8:16

3 Answers 3


AWK may be a bit easier to use:

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

In this code we match two patterns, and if patterns match - we run what's in the curly brackets. For GetCurrentAudioLanguageResponse and status":"GENERAL_ERROR we print "FAIL", and for patterns GetCurrentAudioLanguageResponse and status\":\"OK we print "SUCCESS"


you should check the return status of the grep command. For eg., if my file contains --

  a.b.c.d status:FAILED

running the command

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


I'd offer you to use sed to parse log-file:

sed -n '
        c\getAudioLanguage FAILED
        c\getAudioLanguage SUCCESSFUL
' logfile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .