我有一個看起來像這樣的日志檔案,
<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 元素單獨添加到應用程式組中,因為我使用一個腳本將所有這些值添加到日志檔案中。
我嘗試了很長時間,但仍然無法弄清楚如何做到這一點。
有什么建議嗎?這種方法是可能的還是不可能的?
uj5u.com熱心網友回復:
#!/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

uj5u.com熱心網友回復:
在純 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
在這里,我們將正則運算式與輸入字串匹配。如果匹配,并且我們知道如何更改顏色,我們將第一個替換為<tr>包含顏色的新字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471509.html
上一篇:Bash腳本:在excel中剪切下一行并通過命令列將其粘貼到新列中
下一篇:如何處理時間資料
