将 shell 脚本结果转换为 HTML 表

请帮助我通过电子邮件将 shell 脚本的结果转换为 HTML 表。我编写了一个 shell 脚本,它将给出以下格式的输出。


Directory Name: /a/b/c

Business Date: 20200323

Expected_files_1:2

Expected_files_2:14

Actual_files_1: File count changes each day

Actual_files_2: File count changes each day

Comments of Actual_files_1

Comments of Actual_files_2

Other Comments

我想使用 shell 脚本将上述结果转换为 HTML 表。


预期的 HTML 表格:

https://img.mukewang.com/651d11460001461513220044.jpg

该表应始终包含两行,一行是标题,另一行是脚本中的变量值。第一行是 HTML 表格的标题,它将始终保持不变。第二个表的值每天都会变化。

由于我是 HTML/Shell 脚本编写的初学者,所以我不知道如何编写一个脚本来给出上述 HTML 格式的结果。

任何帮助,将不胜感激


慕尼黑8549860
浏览 170回答 2
2回答

阿晨1998

假设您的输出存储在/tmp/output.txt中,请尝试以下操作:./yourscript.sh > /tmp/output.txtawk -F ':' 'BEGIN { print "<html>\n<body>\n<table>\n\t<tr>" } { print "\t\t<th>"$1"</th>" } END { print "\t</tr>" }' /tmp/output.txtawk -F ':' 'BEGIN { print "\t<tr>" } { print "\t\t<td>"$2"</td>" } END { print "\t</tr>\n</table>\n</body>\n</html>" }' /tmp/output.txt返回<html><body><table>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Directory Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Business Date</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Expected_files_1</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Expected_files_2</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Actual_files_1</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Actual_files_2</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Comments of Actual_files_1</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Comments of Actual_files_2</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Comments Other</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> /a/b/c</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> 20200323</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>2</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>14</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> File count changes each day</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> File count changes each day</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr></table></body></html>如果您确保脚本输出的每一行都具有相同的key: value格式,那么您会更轻松。如果您与我们分享您的脚本,我们可能会为您提供进一步帮助。

当年话下

这是一个两步过程:将列转置为行,使用冒号作为输入/输出分隔符:awk -f tst.awk inputfile.txt > result.txttst.awk 包含以下代码:BEGIN { FS=OFS=":" }{&nbsp; &nbsp; for (rowNr=1;rowNr<=NF;rowNr++) {&nbsp; &nbsp; &nbsp; &nbsp; cell[rowNr,NR] = $rowNr&nbsp; &nbsp; }&nbsp; &nbsp; maxRows = (NF > maxRows ? NF : maxRows)&nbsp; &nbsp; maxCols = NR}END {&nbsp; &nbsp; for (rowNr=1;rowNr<=maxRows;rowNr++) {&nbsp; &nbsp; &nbsp; &nbsp; for (colNr=1;colNr<=maxCols;colNr++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf "%s%s", cell[rowNr,colNr], (colNr < maxCols ? OFS : ORS)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}用于将点 1 的输出转换为 html 表格格式:有一个简单的脚本可用于执行此操作:https:&nbsp;//sourceforge.net/projects/command-output-to-html-table/可用于转换任何命令输出或文件为漂亮的 html 表格格式。您可以为此脚本指定分隔符,包括制表符、换行符等特殊分隔符,并通过顶部的 html 搜索获取 html 表格格式的输出。只需下载脚本,解压并发出以下命令:cat&nbsp;result.txt&nbsp;|&nbsp;{&nbsp;cat&nbsp;;&nbsp;echo&nbsp;;&nbsp;}&nbsp;|&nbsp;./tabulate.sh&nbsp;-d&nbsp;":"&nbsp;-t&nbsp;"My&nbsp;Report"&nbsp;-h&nbsp;"My&nbsp;Report"&nbsp;>&nbsp;output.html这里 : 作为分隔符。我在这里附加了一个示例output.html:https ://sourceforge.net/projects/my-project-files/files/output.html/download
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5