gpt4 book ai didi

html - 使用 BASH 和 AWK 创建 HTML 表格

转载 作者:太空宇宙 更新时间:2023-11-04 13:36:43 24 4
gpt4 key购买 nike

我在创建 html 表以显示文本文件中的统计信息时遇到问题。我确信有 100 种方法可以更好地做到这一点,但这里是:

(以下脚本中的注释显示输出)

#!/bin/bash

function getapistats () {
curl -s http://api.example.com/stats > api-stats.txt
awk {'print $1'} api-stats.txt > api-stats-int.txt
awk {'print $2'} api-stats.txt > api-stats-fqdm.txt
}

# api-stats.txt example
# 992 cdn.example.com
# 227 static.foo.com
# 225 imgcdn.bar.com
# end api-stats.txt example

function get_int () {

for i in `cat api-stats-int.txt`;
do echo -e "<tr><td>${i}</td>";
done
}

function get_fqdn () {

for f in `cat api-stats-fqdn.txt`;
do echo -e "<td>${f}</td></tr>";
done

}

function build_table () {

echo "<table>";
echo -e "`get_int`" "`get_fqdn`";
#echo -e "`get_fqdn`";
echo "</table>";
}

getapistats;

build_table > api-stats.html;

# Output fail :|
# <table>
# <tr><td>992</td>
# <tr><td>227</td>
# <tr><td>225</td><td>cdn.example.com</td></tr>
# <td>static.foo.com</td></tr>
# <td>imgcdn.bar.com</td></tr>

# Desired output:
# <tr><td>992</td><td>cdn.example.com</td></tr>
# ...

最佳答案

这在纯 awk 中相当简单:

curl -s http://api.example.com/stats > api-stats.txt
awk 'BEGIN { print "<table>" }
{ print "<tr><td>" $1 "</td><td>" $2 "</td></tr>" }
END { print "</table>" }' api-stats.txt > api-stats.html

Awk 就是为这种用途而生的。

关于html - 使用 BASH 和 AWK 创建 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11486756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com