将输出文件格式化为bash脚本为HTML表格式

时间:2015-07-06 10:26:19

标签: html linux bash awk format

我有一个从多个源获取数据的脚本,我想将其输出格式化为HTML表格式。

编辑:

目前的格式:

[Environment Name]
[Back end version]
[DB Version]
[event1 status] [event2 status] [event schema] [nodes] [node_no] [vpool] [ver] [node_ip]

目前的清单:

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

我希望将其格式化为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

是否可以使用bash脚本执行此操作?任何帮助将不胜感激我是bash和HTML的新手,所以我被困住了。

我尝试使用答案中的代码:

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"

输出是

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

由于

2 个答案:

答案 0 :(得分:1)

ng-src="{{images}}"

在您尝试编辑问题后,请对此答案发表评论,以便我添加说明。

答案 1 :(得分:0)

使用以下格式可以使用以下格式进入HTML格式:

awk -v header=1 'BEGIN{OFS="\t";  print "<html><body><table>" }
{
        gsub(/</, "\\&lt;")
        gsub(/>/, "\\&gt;")
        gsub(/&/, "\\&gt;")
        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" )

这对于想要转换为HTML的人来说非常有用。