Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用BASH创建HTML表&;AWK_Html_Bash_Function_Awk_Html Table - Fatal编程技术网

使用BASH创建HTML表&;AWK

使用BASH创建HTML表&;AWK,html,bash,function,awk,html-table,Html,Bash,Function,Awk,Html Table,我在创建html表以显示文本文件中的统计信息时遇到问题。我相信有100种方法可以做得更好,但具体如下: (以下脚本中的注释显示输出) #/bin/bash 函数getapists(){ curl-shttp://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.t

我在创建html表以显示文本文件中的统计信息时遇到问题。我相信有100种方法可以做得更好,但具体如下:

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

#/bin/bash
函数getapists(){
curl-shttp://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示例
#992 cdn.example.com
#227 static.foo.com
#225 imgcdn.bar.com
#结束api-stats.txt示例
函数get_int(){
对于“cat api-stats-int.txt”中的i;
do echo-e“${i}”;
完成
}
函数get_fqdn(){
对于“catapi stats fqdn.txt”中的f;
do echo-e“${f}”;
完成
}
函数生成表(){
回声“;
echo-e“`get_int``get_fqdn`”;
#echo-e“`get_fqdn`”;
回声“;
}
Getapists;
build_table>api-stats.html;
#输出失败:|
# 
# 992
# 227
#225cdn.example.com
#static.foo.com
#imgcdn.bar.com
#期望输出:
#992cdn.example.com
# ...
这可以通过bash;)完成

读-u3a和读-u4b;做 回声a$b; 完成3</etc/passwd 4</etc/services 但我的经验是,在bash/awk/等中这样做通常是件坏事

我在代码中使用的特性在bash手册页面中被深深地隐藏着


我建议使用一些真正的语言来处理此类数据,例如:(ruby或python),因为它们更灵活/可读/可维护

这在纯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
curl-shttp://api.example.com/stats >api-stats.txt
awk'开始{print'}
{打印“$1”$2”}
结束{print”“}api-stats.txt>api-stats.html

Awk确实是为这种用途而设计的。

您至少可以用一个Awk来完成

curl -s http://api.example.com/stats | awk '
    BEGIN{print "<table>"} 
    {printf("<tr><td>%d</td><td>%s</td></tr>\n",$1,$2)}
    END{print "</table>"}
' 
curl-shttp://api.example.com/stats |awk'
开始{打印“”}
{printf(“%d%s\n”,$1,$2)}
结束{打印“”}
' 

是的
python
可能是解决问题的方法。谢谢你,巴德;)单引号在大括号外。我知道它是这样工作的,但在下一个简单的复杂性增加时,它失败了。不要在$(cat)中对i使用
-在读取-r时使用
;做完成
。使用
$()
而不是反勾号。您可以使用OSX或在OSX上通过管道将此输出直接传输到浏览器。最后一个
不应该改为`?
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
curl -s http://api.example.com/stats | awk '
    BEGIN{print "<table>"} 
    {printf("<tr><td>%d</td><td>%s</td></tr>\n",$1,$2)}
    END{print "</table>"}
'