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