Linux

使用 awk 向 HTML 表中添加資訊

  • June 22, 2022

我試圖將免費的數字和百分比輸出放在一個表格中,我每封郵件都會發送給自己。很長一段時間以來,我一直試圖將這些組合到一張表中,但似乎沒有什麼能像我認為的那樣起作用。

#!/bin/bash
PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
date="date +%Y%m%d"
file="/usr/local/bin/log/start-info_`$date`.txt"

touch $file
free | awk '/Mem/{printf("Mem: %.1f% "), $3/($2+.0000000001)*100} /Swap/{printf("Swap: %.1f%"), $3/($2+.0000000001)*100}' > /usr/local/bin/test-file.txt
percent=$(cat /usr/local/bin/test-file.txt)
percent="$(echo $percent)"

free -m | awk 'BEGIN{ print("<table border=1>")}
{
print "<tr>"
for ( i = 1; i<=NF ; i++ ) {
printf "<td> %s </td> ", $i
}
printf "<td> %s </td> ", $percent;
print "</tr>"
}
END{
print("</table>")
}' > $file

sed -i 's:<td> total:<td> </td> <td> total:' $file

(
echo "To: myself@example.com"
echo "From: server@example.com"
echo "Subject: startinformation server"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html charset=US-ASCII"
cat $file
echo
) | /usr/sbin/sendmail myself@example.com

$percent 的輸出不在郵件中,它只是在單個單元格中重複先前的數據。

在此處輸入圖像描述

如果我把輸出 $ percent directly into the mail with “echo $ percent >> $file”,然後它正確顯示為“Mem: 16.0% Swap: 0.0%”,但我需要它在表中。

編輯:這是免費 -m 的範例:

$ free -m                                                                                                                 
             total        used        free      shared  buff/cache   available
Mem:           3873         616         898           0        2358        2990
Swap:          3872           0        3872

$ free                                                                                                                    
             total        used        free      shared  buff/cache   available
Mem:        3966276      630492     1045640         956     2290144     3062460
Swap:       3965948         780     3965168

在第一個文件中(我一開始忘了添加)是 HTML 程式碼:

<table border=1>
<tr>
<td> </td> <td> total </td> <td> used </td> <td> free </td> <td> shared </td> <td> buff/cache </td> <td> available </td> <td>               total        used    >
<tr>
<td> Mem: </td> <td> 3873 </td> <td> 619 </td> <td> 597 </td> <td> 0 </td> <td> 2656 </td> <td> 2987 </td> <td> Mem:           3873         619         597      >
<tr>
<td> Swap: </td> <td> 3872 </td> <td> 0 </td> <td> 3872 </td> <td> Swap:          3872           0        3872 </td> </tr>
</table>

第二個文件中只有第一個 awk 的輸出:

Mem: 16.0% Swap: 0.0%

我正在嘗試做的是將第二個文件的輸出,即 16.0% 和 0.0%,放入如下表中:

<table border=1>
<tr>
<td> </td> <td> total </td> <td> used </td> <td> free </td> <td> shared </td> <td> buff/cache </td> <td> available </td> <td> %used </td> </tr>
<tr>
<td> Mem: </td> <td> 3873 </td> <td> 619 </td> <td> 597 </td> <td> 0 </td> <td> 2656 </td> <td> 2987 </td> <td> 16.0% </td> </tr>
<tr>
<td> Swap: </td> <td> 3872 </td> <td> 0 </td> <td> 3872 </td> <td> 0.0% </td> </tr>
</table>

最好我能說這就是你所需要的,即 1 次呼叫free -m,1 次呼叫awk,並且沒有呼叫sed

free -m |
awk '
   BEGIN { print("<table border=1>") }
   {
       if ( NR == 1 ) {
           $0 = "_" FS $0 FS "%used"
           numCols = NF
           $1 = ""
       }
       else {
           $numCols = sprintf("%0.1f%%", ($2 ? ($3 / $2) * 100 : 0))
       }
       print "<tr>"
       for ( i=1; i<=NF; i++ ) {
           printf "<td>%s</td>%s", $i, (i<NF ? OFS : ORS)
       }
       print "</tr>"
   }
   END { print "</table>" }
'

鑑於free -m您提供的輸出,上面將輸出:

<table border=1>
<tr>
<td></td> <td>total</td> <td>used</td> <td>free</td> <td>shared</td> <td>buff/cache</td> <td>available</td> <td>%used</td>
</tr>
<tr>
<td>Mem:</td> <td>3873</td> <td>616</td> <td>898</td> <td>0</td> <td>2358</td> <td>2990</td> <td>15.9%</td>
</tr>
<tr>
<td>Swap:</td> <td>3872</td> <td>0</td> <td>3872</td> <td></td> <td></td> <td></td> <td>0.0%</td>
</tr>
</table>

引用自:https://unix.stackexchange.com/questions/707092