0

I have a script that gets data from multiple sources and I want to format its output to HTML table format.

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

   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 ? Any help will be appreciated I am new to bash and HTML so I am stuck.

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

3
  • 1
    How is that table html ? Do you mean you want HTML to be generated that will create that table ? Commented Jul 6, 2015 at 10:36
  • I want to convert my list into HTML table format. I have this output being generated on a bash script. Commented Jul 6, 2015 at 10:54
  • probably would be easier to modify the generating bash script which already has the context.. could you post this script Commented Jul 6, 2015 at 11:43

3 Answers 3

1
$ 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.

Sign up to request clarification or add additional context in comments.

3 Comments

I am impressed now I can get a really well format and send to another file. Do you have any idea on how to put this into a HTML table using awk ? Thank you so much for that.
^^ Edit my one liner script above. You can generate HTML without too much efforts.
if you have time could you share the explanation for that code, I am really keen to know what is going on this awk statement. Many Thanks
0

With the format above is possible to get into a HTML format using:

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" )

This could be useful for someone looking to convert into HTML.

Comments

0

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. 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. 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.

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

If the delimiter is a tab character, then change -d " " to -d $'\t' above.

The output file generated is attached here: https://sourceforge.net/projects/my-project-files/files/test.html/download

1 Comment

Can you include the script or a part of the script to explain your answer ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.