簡體   English   中英

在讀取文件/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 報告作為表格。 “AAA”、“BBB”、“CCC”和“DDD”是應用程序組。 這是我在那里使用的循環,

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

但是在報表中創建表時,無法單獨識別應用程序組。 我的意思是它們都打印相同的。 我想分隔應用程序組,因為它可以在報告中單獨進行視覺識別。 我打算使用 CSS 或 bootsrap 樣式,所以如果我可以在渲染到 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 (>= Bash 4) 中:

#! /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[$3]"\">"
          print " <td>"$3"</td>"
          print " <td>"$4"-"$5"</td>"
          print " <td>"$7"</td>"
          print "</tr>"
        }
        END{
          print "</tbody>"
          print "</table>"
}' logfile

在此處輸入圖像描述

使用 XML 解析器和“直接元素構造函數”:

$ 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

使用“計算構造函數”:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM