在shell脚本上需要一些关于awk / grep命令的帮助

时间:2016-02-24 07:33:15

标签: shell awk grep

这是参考Need some help on shell scipt (using grep command)

我有一个日志文件,我正在尝试编写一个shell脚本来查找是否发生暂停/播放(成功/失败)。

脚本应该执行以下操作:

if ("trickModeRequest":{"rate":0.0})
{
   if ("status":"OK")
      printf("Pause Successful");
   else if ("status":"GENERAL_ERROR")
      printf("Pause failed");
   else if ("status":"UNRECOGNIZED REQUEST")
      printf("Pause failed");
}

类似地:

if ("trickModeRequest":{"rate":1.0})
{
   if ("status":"OK")
      printf("Play Successful");
   else if ("status":"GENERAL_ERROR")
      printf("Play failed");
   else if ("status":"UNRECOGNIZED REQUEST")
      printf("Play failed");
}

我的脚本文件:

logfile=$1

awk '/trickModeRequest/ && /status\"\:\"GENERAL_ERROR/{ print "trickModeRequestFAILED" } /trickModeRequest/ && /status\":\"OK/ {print "trickModeRequestSUCCESS"}  ' $logfile

问题在于{" trickModeRequest":{" rate":0.0}或{" trickModeRequest":{" rate": 1.0}没有放在同一段日志中,所以我不知道如何区分暂停/播放......

我的日志文件(logs.txt):

160125-11:11:16.654574 [mod=CARDLESSCA, lvl=DEBUG] [tid=2343] [RECORD]NAGRA_API:vlhal_CasPvrRecordingStart-post handle=0x007288a8
160125-11:11:16.654617 [mod=CARDLESSCA, lvl=INFO] [tid=2343] Recording nagra request for Recording vlhal_CasPvrPlaybackStart 0x7288a8
160125-11:11:16.655113 [mod=DVR, lvl=ERROR] [tid=2343] VLTune::process_buffer:1088(this=0x58ca00) - is_recording=1(was 0), hal_recording=0x7555ddd8
160125-11:11:16.905656 [mod=SYS, lvl=TRACE] [tid=2343] vl_env_get_bool: key=NAGRA.PRM.SUPPORT.ENABLED, result=1
160125-11:11:16.910125 [mod=SYS, lvl=TRACE] [tid=2343] vl_env_get_bool: key=NAGRA.PRM.SUPPORT.ENABLED, result=1
160125-11:11:16.911879 [mod=DVR, lvl=INFO] [tid=2343] HAL_RECORD_GetData:2989(0x7555ddd8) - /data/data/OCAP_MSV/0/0/DEFAULT_RECORDING_VOLUME/dvr/1453720276418.BOTF_Marker(worte=376)
160125-11:11:16.949874 [mod=SYS, lvl=INFO] [tid=2332] Read message '{"event":1,"handler":1,"name":"onRPCCall","params":{"callGUID":"228dd4a5-6727-4525-abce-331c87d54c18","callParams":[**{"trickModeRequest":{"rate":0.0}}**],"class":"com.comcast.xre.events.XRERPCCallInfo","destinationSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d",**"method":"trickModeRequest"**,"sourceSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d"},"phase":"STANDARD","source":1}
'
160125-11:11:16.950180 [mod=SYS, lvl=INFO] [tid=2332] ======= Message is onRPCCall ======>
160125-11:11:16.950326 [mod=SYS, lvl=INFO] [tid=2332] Entering onRPCCallEvent for request ---> trickModeRequest
160125-11:11:16.950621 [mod=SYS, lvl=INFO] [tid=2332] Received json request = {"event":1,"handler":1,"name":"onRPCCall","params":{"callGUID":"228dd4a5-6727-4525-abce-331c87d54c18","callParams":[**{"trickModeRequest":{"rate":0.0}**}],"class":"com.comcast.xre.events.XRERPCCallInfo","destinationSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d","method":"trickModeRequest","sourceSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d"},"phase":"STANDARD","source":1}

160125-11:11:16.950689 [mod=SYS, lvl=INFO] [tid=2332] trickModeRequest: trickModeRequest
160125-11:11:16.950794 [mod=TUNERSOURCE, lvl=INFO] [tid=2332] Setting rate 0.0, skipping 0 ms
160125-11:11:16.950872 [mod=TUNERSOURCE, lvl=INFO] [tid=2332] trickModeRequest() - Sending the TrickMode event
160125-11:11:16.950959 [mod=SYS, lvl=INFO] [tid=2332] PlaybackResponse: entered 0
160125-11:11:16.950994 [mod=MEDIA_SESSION_MGR, lvl=DEBUG] [tid=2331] MediaSessionManager::trickMode() - Handling trick mode
160125-11:11:16.951059 [mod=MEDIA_SESSION_MGR, lvl=DEBUG] [tid=2331] MediaSessionManager::trickMode() - rate = 0.00000, skip = 0 ms
160125-11:11:16.951123 [mod=PLAYER, lvl=TRACE] [tid=2331] PlayerBase(id=56a602d3)::stop - Before event: state = %s
160125-11:11:16.951182 [mod=PLAYER, lvl=DEBUG] [tid=2331] DecodePlayer(id=56a602d3)::DecodePlayerStatePresenting::stop - entering state %s
160125-11:11:16.951295 [mod=PLAYER, lvl=DEBUG] [tid=2331] PlayerBase(id=56a602d3)::PlayerStateBase::waitForExistingWorkerThread - waiting for the previous worker thread to stop
160125-11:11:16.951361 [mod=PLAYER, lvl=DEBUG] [tid=2331] PlayerBase(id=56a602d3)::PlayerStateBase::waitForExistingWorkerThread - done joining
160125-11:11:16.951535 [mod=PLAYER, lvl=TRACE] [tid=2331] PlayerBase(id=56a602d3)::stop - After event: state = %s
160125-11:11:16.951639 [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":"228dd4a5-6727-4525-abce-331c87d54c18","class":"com.comcast.xre.events.XRERPCReturnInfo","destinationSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d",**"method":"trickModeRequest"**,"returnVal":{"class":"com.comcast.parker.SelectServiceSyncResponse","selectServiceStatus":"AWAITING_ASYNC","startTime":1296590759,"**status":"OK"**,"statusMessage":"Tuning to service: "},"sourceSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d"}},"ab00ebea-5f63-4619-9877-273a2bceea1d"],"targetId":1,"targetPath":"","timestamp":0}
 ====
160125-11:11:18.002455 [mod=SYS, lvl=INFO] [tid=2332] Read message '{"event":1,"handler":1,"name":"onRPCCall","params":{"callGUID":"fe11a665-2aad-404c-a887-bd3d967bbea0","callParams":[{**"trickModeRequest":{"rate":1.0}**}],"class":"com.comcast.xre.events.XRERPCCallInfo","destinationSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d",**"method":"trickModeRequest"**,"sourceSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d"},"phase":"STANDARD","source":1}
'
160125-11:11:18.002756 [mod=SYS, lvl=INFO] [tid=2332] ======= Message is onRPCCall ======>
160125-11:11:18.002843 [mod=SYS, lvl=INFO] [tid=2332] Entering onRPCCallEvent for request ---> trickModeRequest
160125-11:11:18.003106 [mod=SYS, lvl=INFO] [tid=2332] Received json request = {"event":1,"handler":1,"name":"onRPCCall","params":{"callGUID":"fe11a665-2aad-404c-a887-bd3d967bbea0","callParams":[{"trickModeRequest":{"rate":1.0}}],"class":"com.comcast.xre.events.XRERPCCallInfo","destinationSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d",**"method":"trickModeRequest"**,"sourceSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d"},"phase":"STANDARD","source":1}

160125-11:11:18.003168 [mod=SYS, lvl=INFO] [tid=2332] trickModeRequest: trickModeRequest
160125-11:11:18.003264 [mod=TUNERSOURCE, lvl=INFO] [tid=2332] Setting rate 1.0, skipping 0 ms
160125-11:11:18.003315 [mod=TUNERSOURCE, lvl=INFO] [tid=2332] trickModeRequest() - Sending the TrickMode event
160125-11:11:18.003387 [mod=SYS, lvl=INFO] [tid=2332] PlaybackResponse: entered 0
160125-11:11:18.003952 [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":"fe11a665-2aad-404c-a887-bd3d967bbea0","class":"com.comcast.xre.events.XRERPCReturnInfo","destinationSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d",**"method":"trickModeRequest"**,"returnVal":{"class":"com.comcast.parker.SelectServiceSyncResponse","selectServiceStatus":"AWAITING_ASYNC","startTime":1296590759,**"status":"OK"**,"statusMessage":"Tuning to service: "},"sourceSessionGUID":"ab00ebea-5f63-4619-9877-273a2bceea1d"}},"ab00ebea-5f63-4619-9877-273a2bceea1d"],"targetId":1,"targetPath":"","timestamp":0}
 ====
160125-11:11:18.004268 [mod=MEDIA_SESSION_MGR, lvl=DEBUG] [tid=2331] MediaSessionManager::trickMode() - Handling trick mode
160125-11:11:18.004370 [mod=MEDIA_SESSION_MGR, lvl=DEBUG] [tid=2331] MediaSessionManager::trickMode() - rate = 1.00000, skip = 0 ms
160125-11:11:18.004429 [mod=PLAYER, lvl=DEBUG] [tid=2331] PlayerBase(id=56a602d3)::PlayerStateBase::waitForExistingWorkerThread - waiting for the previous worker thread to stop
160125-11:11:18.010346 [mod=SYS, lvl=INFO] [tid=2332]  ====== Response of 685 bytes sent ======
160125-11:11:18.010529 [mod=SYS, lvl=INFO] [tid=2332] ProcessJsonData: returned success
160125-11:11:18.010633 [mod=SYS, lvl=INFO] [tid=2332] === OnConnect returned PS_SUCCESS
160125-11:11:18.011512 [mod=DVR, lvl=INFO] [tid=2349] HAL_PLAYBACK_Start:3789(0x596290) - NEXUS_Playpump_Open(NEXUS_ANY_ID, 0xc7e66800) returns 0x54052014)
160125-11:11:18.011555 [mod=SYS, lvl=TRACE] [tid=2343] vl_env_get_bool: key=NAGRA.PRM.SUPPORT.ENABLED, result=1
160125-11:11:18.013621 [mod=SYS, lvl=TRACE] [tid=2349] vl_env_get_bool: key=FEATURE.TRICKPLAY.CHANNEL_CHANGE.SLOW_MOTION, result=0
160125-11:11:18.013727 [mod=SYS, lvl=TRACE] [tid=2349] vl_env_get_bool: key=FEATURE.PARENTALCONTROL.POLICY.STRICT, result=1
160125-11:11:18.013786 [mod=DVR, lvl=INFO] [tid=2349] HAL_Playback_Init:355() - Using FEATURE.PARENTALCONTROL.POLICY.STRICT = true

1 个答案:

答案 0 :(得分:1)

按照您的逻辑,您可以使用变量type来跟踪调用。实际上,在您的日志中,您总是有一行列出Received json: ... callParams 之前具有状态的响应。如果你总是有这个序列(很难用你的例子猜测),你可以这样做:

  awk 'BEGIN { type=""; } /Received json/ && /"rate":1.0/ {type="play"} /Received json/ && /"rate":0.0/ {type="pause"} <your code>' $logfile

然后,您可以在打印的消息中使用type,这应该是播放或暂停。

完整代码:

awk 'BEGIN{ type=""; } /trickModeRequest/ && /status\"\:\"GENERAL_ERROR/{ print type, "FAILED" } /trickModeRequest/ && /status\":\"OK/ {print type, "SUCCESSFUL"} /Received json/ && /"rate":1.0/ {type="play"} /Received json/ && /"rate":0.0/ {type="pause"} ' $logfile

输出:

pause SUCCESSFUL
play SUCCESSFUL