vendredi 24 mai 2019

Validate log file using shell

I want to validate a log file based on a reference file, I worked on a script but, it is not beautiful and is not optimal:

For each line I want to check the value of the fields, - the field 7 equal to 1 I have to check columns 16 and 17 - the field 7 equal to 2 I have to check columns 25 and 27 and 30 - the field 7 equal to 3 I have to check columns 18 and 24 and 31 etc..

#!/bin/bash

LOG=SMS.log



   awk -F\| ' {s=""}
    $4!=0  {printf "API has wrong value"; s="; " }
    $8=="" { printf "%sApplicationID is empty", s; s="; " }
    $9=="" { printf "%shttp request method is empty", s; s="; " }
    $7=="" { printf "%sOperationID is empty", s; s="; " }
    $13 !~ /0|1|2/ {printf "%sresult(0,1,2) has a wrong value", s; s="; " }

        # 1:create SMS

    $7=="1" && $18=="" {printf "%sSender is missing", s; s="; " }
    $7=="1" && $18 ~ /\/tel\:\+\*\*/ {printf "%sSender is cyphred !", s; s="; " }
    $7=="1" && $20=="" {printf "%sAddress is missing", s; s="; " }
    $7=="1" && $20 ~ /\/tel\:\+[0-9]/ {printf "%sAddress(es) is not cyphred", s; s="; " }
    $7=="1" && $10 ~ /\/tel\:\+\*\*/ {printf "%sSender is cyphred on URI !", s; s="; " }

        ## 2:subscribe 

    $7=="2" && $25=="" {printf "%sdestination is missing", s; s="; " }
    $7=="2" && $16=="201" && $27="" {printf "%sresourceId is missing", s; s="; "}

        #3:unsubscribe 
    $7=="2" && $16=="201" && $25="" {printf "%sresource is missing", s; s="; "}

    s { printf "\n"}
    s
     {printf "\n"}
' $LOG

Is it possible to update the code to be more optimal and beautiful.

Aucun commentaire:

Enregistrer un commentaire