Home > Software engineering >  I want to modify the content of log file using shell script
I want to modify the content of log file using shell script

Time:06-28

log file looks like

07-15:01:56 ClientID="422314" ComplianceID="skdjiowecn33nf" ExecID="343ndndn8c" Type="NOK"
07-15:01:57 ClientID="422e214" ComplianceID="skdjiowecn33nf" ExecID="34en3fn" Type="NOK"
07-15:01:58 ClientID="42124" ComplianceID="skdjiowecn33nf" ExecID="4n23n43" d" Type="NOK"

Now I have to modify the content of it like below . I need to add start html tag and copy ExecID to TRANID and then modify the value of ExecID by appending "PUSH_" . also I need to change the value type from NOK to OK

07-15:01:55 ClientID="42454214" CompID="skdjiowecn33nf" ExecID="PUCH_dsjkh323f0d" Type="OK" TRANID="dsjkh323f0d"
</START>
<START>
07-15:01:56 ClientID="422314" CompID="skdjiowecn33nf" ExecID="PUSH_343ndndn8c" Type="OK" TRANID="343ndndn8c"
</START>
<START>
07-15:01:57 ClientID="422e214" CompID="skdjiowecn33nf" ExecID="PUSH_34en3fn" Type="OK" TRANID="34en3fn"
</START>
<START>
07-15:01:58 ClientID="42124" CompID="skdjiowecn33nf" ExecID="PUSH_4n23n43 d" Type="OK" TRANID="4n23n43"
</START>

script should handle huge block of logs

CodePudding user response:

$ awk '{ 
   printf "%s\n%s\n%s\n", 
     "<START>", 
     gensub(/(.*) (ExecID=)"(.*)" (.*)/,"\\1 \\2\"PUSH_\\3\" type=\"OK\" TRANID=\"\\3\"", 1, $0), 
     "</START>"
}' logfile


<START>
07-15:01:56 ClientID="422314" ComplianceID="skdjiowecn33nf" ExecID="PUSH_343ndndn8c" type="OK" TRANID="343ndndn8c"
</START>
<START>
07-15:01:57 ClientID="422e214" ComplianceID="skdjiowecn33nf" ExecID="PUSH_34en3fn" type="OK" TRANID="34en3fn"
</START>
<START>
07-15:01:58 ClientID="42124" ComplianceID="skdjiowecn33nf" ExecID="PUSH_4n23n43" type="OK" TRANID="4n23n43"
</START>

07-15:01:56 ClientID="422314" ComplianceID="skdjiowecn33nf" ExecID="343ndndn8c" Type="NOK"
----------------------------------------------------------- ------- ----------  ----------
                            |                                  |    | (.*) group3 \\3 | (.*) group4 \\4
match-groups         (.*) group1 \\1                           |
                                                            (ExecID=) group2 \\2                   
  • Related