Home > Net >  change values while reading a file / shell scripting loops ,
change values while reading a file / shell scripting loops ,

Time:05-10

I have a log file that looks like this,

<tr><td>AAA-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr><td>AAA-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr><td>BBB-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr><td>BBB-application-04 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr><td>CCC-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  
<tr><td>CCC-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  
<tr><td>CCC-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  

I am looping through this log file and generating a HTML report as a table. "AAA" , "BBB" , "CCC" and "DDD" are the application groups. Here is the loop that I used there,

while read line; do
for item in $line; do 
     echo $item
done
done < filename.log 

But when the table is created in the report the application groups cannot be identified seperately. I mean they all prints the same. I want to seperate the application groups as it can be visually identified seperately in the report. im going to use CSS or bootsrap styles, so its is better if I can change the class of while rendering to the HTML.

So the end result should be like,

<tr ><td>AAA-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr ><td>AAA-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr ><td>BBB-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr ><td>BBB-application-04 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr ><td>CCC-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  
<tr ><td>CCC-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  
<tr ><td>CCC-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>

so this means "AAA" applications will be red "BBB" application will be green "CCC" applicattion will be yellow

When creating the log file I cannot add HTML elements seperately to the application groups , because I use a one script to take all of those values to the log file.

I tried this so much for a long time and still couldnt figure out how to do this.

Any suggestion on doing this ?? is this approch is possible or impossible to do ??

CodePudding user response:

#!/bin/bash
cat << ==CSS==
<style>
        tr.red{
                background: red;
                color: white;
        }
        tr.yellow {
                background: yellow;
        }
        tr.green{
                background: green;
                color: white;
        }
</style>
==CSS==

awk -F"[-<>]" '
        BEGIN{
          classes["AAA"] = "red"
          classes["BBB"] = "green"
          classes["CCC"] = "yellow"
          print "<table border=1>"
          print "<thead><tr><th>Group</th><th>Application</th><th>Reason</th></tr></thead>"
          print "<tbody>"
        }
        {
          print "<tr class=\""classes[$3]"\">"
          print " <td>"$3"</td>"
          print " <td>"$4"-"$5"</td>"
          print " <td>"$7"</td>"
          print "</tr>"
        }
        END{
          print "</tbody>"
          print "</table>"
}' logfile

enter image description here

CodePudding user response:

In pure Bash (>= Bash 4) :

#! /usr/bin/env bash

declare -A colors=(
    [AAA]=''
    [BBB]=''
    [CCC]=''
)

regex="^<tr><td>(...)-.*</td></tr>$"

while read -r line; do
    if [[ $line =~ $regex ]]; then
        entry=${BASH_REMATCH[1]}
        [[ -v colors[$entry] ]] && line=${line/<tr>/<tr ${colors[$entry]}>}
    fi
    printf "%s\n" "$line"
done < filename.log

Here, we match a regexp with the input string. If there is a match, and we know how to change the color, we substitute the first <tr> with a new string containing the color.

  • Related