[英]Format output file from a bash script into HTML table format
I have a script that gets data from multiple sources and I want to format its output to HTML table format.我有一个从多个来源获取数据的脚本,我想将其输出格式化为 HTML 表格格式。
Edited:编辑:
The format at the moment:目前的格式:
[Environment Name]
[Back end version]
[DB Version]
[event1 status] [event2 status] [event schema] [nodes] [node_no] [vpool] [ver] [node_ip]
The list at the moment:目前名单:
grid-dev
BE version: 6.0
Database version: 10
DISABLED DISABLED dev_1 3 01 1 10.0.19-MariaDB 10.101.666.11:3306
grid-test
BE version: 7.0
Database version: 11
ENABLED ENABLED test_1 2 02 4 10.0.17-MariaDB 10.108.777.14:3306
grid-test
BE version: 7.0
Database version: 11
SLAVESIDE_DISABLE SLAVESIDE_DISABLE test_2 1 02 3 10.0.17-MariaDB 10.108.777.47:3306
grid-staging
BE version: 6.0
Database version: 10
DISABLED DISABLED staging_1 2 02 4 10.0.18-MariaDB 10.109.888.22:3306
and I want to format it to HTML table in something like this我想以这样的方式将其格式化为 HTML 表格
ENVIRONMENT BACKEND_VERSION DB_VERSION EVENT1 EVENT2 SCHEMA NODES NODE_NO VPOOL VERSION IP
----------------------------------------------------------------------------------------------------------------------------------------------------------
grid-dev 6 10 DISABLED DISABLED dev_1 3 01 1 10.0.19-MariaDB 10.101.666.11:3306
grid-test 7 11 ENABLED ENABLED test_1 2 02 4 10.0.17-MariaDB 10.108.777.14:3306
grid-test 7 11 SLAVES... SLAVESI... test_2 2 01 3 10.0.17-MariaDB 10.108.777.47:3306
grid-staging 6 10 DISABLED DISABLED stag_1 2 02 4 10.0.18-MariaDB 10.109.888.22:3306
Is it possible to do it using bash script ?是否可以使用 bash 脚本来完成? Any help will be appreciated I am new to bash and HTML so I am stuck.任何帮助将不胜感激我是 bash 和 HTML 的新手,所以我被卡住了。
My attemp using the code on the answer:我在答案中使用代码的尝试:
awk 'BEGIN{print "ENVIRONMENT BACKEND_VERSION DB_VERSION EVENT1 EVENT2 SCHEMA NODES NODE_NO VPOOL VERSION IP" } NF==1{env=$0; t=1; next;} t==1{t++; be=$3; next;} t==2{t++; db=$3; next;} t==3{printf "%s %s %s %s\n", env, be, db, $0; env="#";be="#";db="#";}' < "$output" | column -t | tr '#' ' ' >> "$dbstats"
The out put is输出是
ENVIRONMENT BACKEND_VERSION DB_VERSION EVENT1 EVENT2 SCHEMA NODES NODE_NO VPOOL VERSION IP
grid-dev56.0 136 grid_dev Database version: 138
DISABLED DISABLED grid_systest 3 03 1 10.0.19-MariaDBgrid-systest56.0
Database version: 138
SLAVESIDE_DISABLED SLAVESIDE_DISABLED grid_systest 3 01 1 10.0.19-MariaDBgrid-systest56.0
Database version: 138
SLAVESIDE_DISABLED SLAVESIDE_DISABLED grid_systest 3 02 1 10.0.19-MariaDBgrid-staging56.0
Database version: 136
SLAVESIDE_DISABLED SLAVESIDE_DISABLED grid_staging 3 03 1 10.0.19-MariaDBgrid-staging56.0
Database version: 136
SLAVESIDE_DISABLED SLAVESIDE_DISABLED grid_staging 3 02 1 10.0.19-MariaDBgrid-staging56.0
Database version: 136
ENABLED ENABLED grid_staging 3 01 1 10.0.19-MariaDBgrid-production56.0
Database version: 136
SLAVESIDE_DISABLED SLAVESIDE_DISABLED grid_production 3 03 1 10.0.19-MariaDBgrid-production56.0
Database version: 136
SLAVESIDE_DISABLED SLAVESIDE_DISABLED grid_production 3 02 1 10.0.19-MariaDBgrid-production56.0
Database version: 136
DISABLED SLAVESIDE_DISABLED grid_production 3 01 1 10.0.19-MariaDB
Thanks谢谢
$ awk 'BEGIN{print "Envirnoment BackEndVersion DBVersion EventName Status Schema" } NF==1{env=$0; t=1; next;} t==1{t++; be=$3; next;} t==2{t++; db=$3; next;} t==3{printf "%s %s %s %s\n", env, be, db, $0; env="#";be="#";db="#";}' <input_file | column -t | tr '#' ' '
Envirnoment BackEndVersion DBVersion EventName Status Schema
grid-dev 6.0 10 swap DISABLED dev_1
busy DISABLED dev_1
grid-test 7.0 11 swap ENABLED test_1
busy ENABLED test_1
grid-staging 6.0 10 swap DISABLED staging_1
busy DISABLED staging_1
grid-production 5.0 9 swap ENABLED prod
busy ENABLES prod
After you edit your question with your attempts, Please comment on this answer, so that I will add explanation. 在尝试编辑问题后,请对此答案发表评论,以便我添加解释。
With the format above is possible to get into a HTML format using: 使用上述格式,可以使用以下格式将其转换为HTML格式:
awk -v header=1 'BEGIN{OFS="\t"; print "<html><body><table>" }
{
gsub(/</, "\\<")
gsub(/>/, "\\>")
gsub(/&/, "\\>")
print "\t<tr>"
for(f = 1; f <=NF; f++) {
if(NR == 1 && header) {
printf "\t\t<th>%s</th>\n", $f
}
else printf "\t\t<td>%s</td>\n", $f
}
print "\t</tr>"
}
END {
print "</table></body></html>"
}' "$FORMATED_TABLE" )
This could be useful for someone looking to convert into HTML. 这对于希望转换为HTML的人可能很有用。
I know it's a late answer to this question, but will help those googling for a solution, for converting bash command output to html table format.我知道这是对这个问题的迟到答案,但会帮助那些在谷歌上搜索解决方案的人,将 bash 命令输出转换为 html 表格式。 There is an easy script available to do this at : https://sourceforge.net/projects/command-output-to-html-table/ which can be used to convert any command output or file to a nice html table format.有一个简单的脚本可用于执行此操作: https://sourceforge.net/projects/command-output-to-html-table/可用于将任何命令输出或文件转换为漂亮的 html 表格格式。 You can specify the delimiter to this script, including special ones like tabs, newlines etc. and get the output in html table format with a html search at the top.您可以为此脚本指定分隔符,包括特殊的分隔符,如制表符、换行符等,并以 html 表格格式获取输出,并在顶部进行 html 搜索。
Just download the script, extract it and issue the following command :只需下载脚本,解压缩并发出以下命令:
cat test.txt | { cat ; echo ; } | ./tabulate.sh -d " " -t "My Report" -h "My Report" > test.html
This assumes that fields are separated by a space character, as specified by the other solution : https://stackoverflow.com/a/31245048/16923394这假设字段由空格字符分隔,如其他解决方案所指定: https : //stackoverflow.com/a/31245048/16923394
If the delimiter is a tab character, then change -d " " to -d $'\\t' above.如果分隔符是制表符,则将上面的 -d " " 更改为 -d $'\\t'。
The output file generated is attached here: https://sourceforge.net/projects/my-project-files/files/test.html/download生成的输出文件附在此处: https : //sourceforge.net/projects/my-project-files/files/test.html/download
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.