读取文件时更改值/shell 脚本循环,

change values while reading a file / shell scripting loops ,

我有一个如下所示的日志文件,

<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>  

我正在遍历此日志文件并生成 HTML 报告作为 table。 “AAA”、“BBB”、“CCC”和“DDD”是应用程序组。 这是我在那里使用的循环,

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

但是在报告中创建table时,应用程序组无法单独识别。 我的意思是它们都打印相同。我想将应用程序组分开,因为它可以在报告中单独视觉识别。 我打算使用 CSS 或 bootsrap 样式,所以如果我可以将渲染时的 class 更改为 HTML.

会更好

所以最终结果应该是这样的,

<tr class="red"><td>AAA-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr class="red"><td>AAA-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr class="green"><td>BBB-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr class="green"><td>BBB-application-04 <br> Qality_gate_failed_reason:xxxxxxx </td></tr> 
<tr class="yellow"><td>CCC-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  
<tr class="yellow"><td>CCC-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  
<tr class="yellow"><td>CCC-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>

所以这意味着“AAA”应用程序将是红色的 “BBB”应用程序将是绿色的 “CCC”应用程序将是黄色的

创建日志文件时,我无法将 HTML 元素单独添加到应用程序组中,因为我使用一个脚本将所有这些值添加到日志文件中。

我试了很久还是不知道怎么做。

有什么建议吗??这个方法可行还是不可行??

纯Bash(>=Bash4):

#! /usr/bin/env bash

declare -A colors=(
    [AAA]="red"
    [BBB]="green"
    [CCC]="yellow"
)

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 class=\"${colors[$entry]}\">}
    fi
    printf "%s\n" "$line"
done < filename.log

在这里,我们将正则表达式与输入字符串匹配。如果匹配,并且我们知道如何更改颜色,我们将第一个 <tr> 替换为包含颜色的新字符串。

例如,使用以下输入:

<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>  
<tr><td>DDD-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  
<tr><td>DDD-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  
<tr><td>DDD-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>  

你会得到:

<tr class="red"><td>AAA-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
<tr class="red"><td>AAA-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
<tr class="green"><td>BBB-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
<tr class="green"><td>BBB-application-04 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
<tr class="yellow"><td>CCC-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
<tr class="yellow"><td>CCC-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
<tr class="yellow"><td>CCC-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
<tr><td>DDD-application-01 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
<tr><td>DDD-application-02 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
<tr><td>DDD-application-03 <br> Qality_gate_failed_reason:xxxxxxx </td></tr>
#!/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[]"\">"
          print " <td>""</td>"
          print " <td>""-""</td>"
          print " <td>""</td>"
          print "</tr>"
        }
        END{
          print "</tbody>"
          print "</table>"
}' logfile

随着 XML-parser and "direct element constructors":

$ xidel -s filename.log -e '
  x:replace-nodes(
    tr,
    function($x){
      $x/<tr class="{map{"AAA":"red","BBB":"green","CCC":"yellow"}(substring(td,1,3))}">{node()}</tr>
    }
  )
' --output-node-format=html

与“computed constructors”:

$ xidel -s filename.log -e '
  x:replace-nodes(
    tr,
    function($x){
      $x/element tr {
        attribute class {
          {"AAA":"red","BBB":"green","CCC":"yellow"}(substring(td,1,3))
        },
        node()
      }
    }
  )
' --output-node-format=html

两种情况下的输出:

<tr class="red"><td>AAA-application-01 <br> Qality_gate_failed_reason:xxxxxxx </br></td></tr>
<tr class="red"><td>AAA-application-02 <br> Qality_gate_failed_reason:xxxxxxx </br></td></tr>
<tr class="green"><td>BBB-application-03 <br> Qality_gate_failed_reason:xxxxxxx </br></td></tr>
<tr class="green"><td>BBB-application-04 <br> Qality_gate_failed_reason:xxxxxxx </br></td></tr>
<tr class="yellow"><td>CCC-application-01 <br> Qality_gate_failed_reason:xxxxxxx </br></td></tr>
<tr class="yellow"><td>CCC-application-02 <br> Qality_gate_failed_reason:xxxxxxx </br></td></tr>
<tr class="yellow"><td>CCC-application-03 <br> Qality_gate_failed_reason:xxxxxxx </br></td></tr>