Add information to HTML table with awk

I am trying to get the numerical and percentage outputs of free together in a table, which I send to myself per mail. I have tried to combine these into one table for quite a while, but nothing seems to work as I think it should.

#!/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

The output of $percent isn’t in the mail, it just repeats the previous data in a single cell.

enter image description here

If I put the output of $percent directly into the mail with "echo $percent >> $file", then it shows correctly as "Mem: 16.0% Swap: 0.0%", but I need it to be in the table.

EDIT:
Here is an example of free -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

In the first file (which I forgot to add at first) is the HTML code:

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

There is only the output of the first awk in the second file:

Mem: 16.0% Swap: 0.0%

What I’m trying to do, is get the output of the second file, so the 16.0% and 0.0%, into the table like this:

<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>
Asked By: user530909

||

Best I can tell this is all you need, i.e. 1 call to free -m, 1 call to awk, and no call to 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>" }
'

Given the output of free -m you provided, the above will output:

<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>
Answered By: Ed Morton
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.