Skip to main content
added 52 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

@Fravadona's answer is good, here's just an alternative approach, using any POSIX awk, to consider that's IMO not anysignificantly better aside from a couple of cosmetic differences, it/bells and whistles. It mainly just isolates each specific part of the output a bit further by providing a separate function for each level/segment of HTML (html/body/table/rows/row/cells/cell) which might make it a bit easier and less error prone to modify that one part of itif necessary in future:

$ cat tst.sh
#!/usr/bin/env bash

awk '

    BEGIN {
        FS = "[|]"
        Indent = -2
        prt_html()
        exit
    }

    function prt_html() {
        prt_beg("<html>")
        prt_body()
        prt_end("</html>")
    }

    function prt_body() {
        prt_beg("<body>")
        prt_table()
        prt_end("</body>")
    }

    function prt_table() {
        prt_beg("<table border=1 cellspacing=0 cellpadding=3>")
        numHdrCells = prt_rowprt_hdr_row(0,"SID|PID|Username|Database|Hostname|Program|Connected|Idle Time|Query Time|EST COST|SEQ SCAN|Query")
        prt_rowsprt_data_rows(numHdrCells)
        prt_end("</table>")
    }

    function prt_rowsprt_data_rows(numHdrCells,      line) {
        while ( (getline line) > 0 ) {
            prt_rowprt_data_row(numHdrCells,line)
        }
    }

    function prt_rowprt_hdr_row(numHdrCells,s,     numCells) {
        prt_beg("<tr>")
        numCells = prt_cellsprt_hdr_cells(numHdrCells,s)
        prt_end("</tr>")
        return numCells
    }

    function prt_cellsprt_data_row(numHdrCells,s,) {
   cellType,numCells,f,i) {
    prt_beg("<tr>")
      numCells = splitprt_data_cells(snumHdrCells,s)
 f, /[|]      prt_end("</tr>")
    }

    iffunction prt_hdr_cells(s, numHdrCells == 0 numCells,f,i) {
        numCells = split(s, f)
 # We are printing the header row
  for (i = 1; i <= numCells; i++) {
  cellType = "th"
        }prt_cell("th",f[i])
        else {}
        return numCells
   # We}

 are printing a datafunction row
prt_data_cells(numHdrCells,s,    f,i) {
       cellType =split(s, "td"f)
            # Force the number of cells printed for each data row
            # to match the number of cells printed for the header.
            numCells = numHdrCells
        }
        for (i = 1; i <= numCells;numHdrCells; i++) {
            prt_cell(cellType"td",f[i])
        }
        return numCells
    }

    function prt_cell(cellType,s) {
        Indent += 2
        # Strip any leading/trailing white space around this cell
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
        prt_indented("<" cellType ">" escape_html(s) "</" cellType ">")
        Indent -= 2
    }

    function escape_html(s) {
        gsub(/&/, "\\&amp;", s)
        gsub(/</, "\\&lt;", s)
        gsub(/>/, "\\&gt;", s)
        return s
    }

    function prt_beg(s) { Indent += 2; prt_indented(s) }
    function prt_end(s) { prt_indented(s); Indent -= 2 }
    function prt_indented(s) { printf("%*s%s\n", Indent, "", s) }

' "${@:--}"

@Fravadona's answer is good, here's just an alternative approach to consider that's IMO not any better aside from a couple of cosmetic differences, it just isolates each specific part of the output a bit further by providing a separate function for each level of HTML (html/body/table/rows/row/cells/cell) which might make it a bit less error prone to modify that one part of it in future:

$ cat tst.sh
#!/usr/bin/env bash

awk '

    BEGIN {
        Indent = -2
        prt_html()
        exit
    }

    function prt_html() {
        prt_beg("<html>")
        prt_body()
        prt_end("</html>")
    }

    function prt_body() {
        prt_beg("<body>")
        prt_table()
        prt_end("</body>")
    }

    function prt_table() {
        prt_beg("<table border=1 cellspacing=0 cellpadding=3>")
        numHdrCells = prt_row(0,"SID|PID|Username|Database|Hostname|Program|Connected|Idle Time|Query Time|EST COST|SEQ SCAN|Query")
        prt_rows(numHdrCells)
        prt_end("</table>")
    }

    function prt_rows(numHdrCells,      line) {
        while ( (getline line) > 0 ) {
            prt_row(numHdrCells,line)
        }
    }

    function prt_row(numHdrCells,s,     numCells) {
        prt_beg("<tr>")
        numCells = prt_cells(numHdrCells,s)
        prt_end("</tr>")
        return numCells
    }

    function prt_cells(numHdrCells,s,    cellType,numCells,f,i) {
        numCells = split(s, f, /[|]/)
        if ( numHdrCells == 0 ) {
            # We are printing the header row
            cellType = "th"
        }
        else {
            # We are printing a data row
            cellType = "td"
            # Force the number of cells printed for each data row
            # to match the number of cells printed for the header.
            numCells = numHdrCells
        }
        for (i = 1; i <= numCells; i++) {
            prt_cell(cellType,f[i])
        }
        return numCells
    }

    function prt_cell(cellType,s) {
        Indent += 2
        # Strip any leading/trailing white space around this cell
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
        prt_indented("<" cellType ">" escape_html(s) "</" cellType ">")
        Indent -= 2
    }

    function escape_html(s) {
        gsub(/&/, "\\&amp;", s)
        gsub(/</, "\\&lt;", s)
        gsub(/>/, "\\&gt;", s)
        return s
    }

    function prt_beg(s) { Indent += 2; prt_indented(s) }
    function prt_end(s) { prt_indented(s); Indent -= 2 }
    function prt_indented(s) { printf("%*s%s\n", Indent, "", s) }

' "${@:--}"

@Fravadona's answer is good, here's just an alternative approach, using any POSIX awk, to consider that's IMO not significantly better aside from a couple of cosmetic differences/bells and whistles. It mainly just isolates each specific part of the output by providing a separate function for each level/segment of HTML which might make it a bit easier and less error prone to modify if necessary in future:

$ cat tst.sh
#!/usr/bin/env bash

awk '

    BEGIN {
        FS = "[|]"
        Indent = -2
        prt_html()
        exit
    }

    function prt_html() {
        prt_beg("<html>")
        prt_body()
        prt_end("</html>")
    }

    function prt_body() {
        prt_beg("<body>")
        prt_table()
        prt_end("</body>")
    }

    function prt_table() {
        prt_beg("<table border=1 cellspacing=0 cellpadding=3>")
        numHdrCells = prt_hdr_row("SID|PID|Username|Database|Hostname|Program|Connected|Idle Time|Query Time|EST COST|SEQ SCAN|Query")
        prt_data_rows(numHdrCells)
        prt_end("</table>")
    }

    function prt_data_rows(numHdrCells, line) {
        while ( (getline line) > 0 ) {
            prt_data_row(numHdrCells,line)
        }
    }

    function prt_hdr_row(s,     numCells) {
        prt_beg("<tr>")
        numCells = prt_hdr_cells(s)
        prt_end("</tr>")
        return numCells
    }

    function prt_data_row(numHdrCells,s) {
        prt_beg("<tr>")
        prt_data_cells(numHdrCells,s)
        prt_end("</tr>")
    }

    function prt_hdr_cells(s,    numCells,f,i) {
        numCells = split(s, f)
        for (i = 1; i <= numCells; i++) {
            prt_cell("th",f[i])
        }
        return numCells
    }

    function prt_data_cells(numHdrCells,s,    f,i) {
        split(s, f)
        # Force the number of cells printed for each data row
        # to match the number of cells printed for the header.
        for (i = 1; i <= numHdrCells; i++) {
            prt_cell("td",f[i])
        }
    }

    function prt_cell(cellType,s) {
        Indent += 2
        # Strip any leading/trailing white space around this cell
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
        prt_indented("<" cellType ">" escape_html(s) "</" cellType ">")
        Indent -= 2
    }

    function escape_html(s) {
        gsub(/&/, "\\&amp;", s)
        gsub(/</, "\\&lt;", s)
        gsub(/>/, "\\&gt;", s)
        return s
    }

    function prt_beg(s) { Indent += 2; prt_indented(s) }
    function prt_end(s) { prt_indented(s); Indent -= 2 }
    function prt_indented(s) { printf("%*s%s\n", Indent, "", s) }

' "${@:--}"
added 305 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
$ cat tst.sh
#!/usr/bin/env bash

awk '

    BEGIN {
        Indent = -2
        prt_html()
        exit
    }

    function prt_html() {
        prt_beg("<html>")
        prt_body()
        prt_end("</html>")
    }

    function prt_body() {
        prt_beg("<body>")
        prt_table()
        prt_end("</body>")
    }

    function prt_table() {
        prt_beg("<table border=1 cellspacing=0 cellpadding=3>")
        numHdrCells = prt_row(0,"SID|PID|Username|Database|Hostname|Program|Connected|Idle Time|Query Time|EST COST|SEQ SCAN|Query")
        prt_rows(numHdrCells)
        prt_end("</table>")
    }

    function prt_rows(numHdrCells,      line) {
        while ( (getline line) > 0 ) {
            prt_row(numHdrCells,$0line)
        }
    }

    function prt_row(numHdrCells,s,    n numCells) {
        prt_beg("<tr>")
        nnumCells = prt_cells(numHdrCells,s)
        prt_end("</tr>")
        return nnumCells
    }

    function prt_cells(numHdrCells,s,    ncellType,numCells,f,i) {
        nnumCells = split(s, f, /[|]/)
        if ( numHdrCells !=== 0 ) {
            # We are printing the header row
            cellType = "th"
        }
        else {
            # We are printing a data row
            cellType = "td"
            # Force the number of cells printed for each data row
            # to match the number of cells printed for the header.
            nnumCells = numHdrCells
        }
        for (i = 1; i <= n;numCells; i++) {
            prt_cell(cellType,f[i])
        }
        return nnumCells
    }

    function prt_cell(cellType,s) {
        Indent += 2
        # Strip any leading/trailing white space around this cell
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
        prt_indented("<td>""<" cellType ">" escape_html(s) "</td>"" cellType ">")
        Indent -= 2
    }

    function escape_html(s) {
        gsub(/&/, "\\&amp;", s)
        gsub(/</, "\\&lt;", s)
        gsub(/>/, "\\&gt;", s)
        return s
    }

    function prt_beg(s) { Indent += 2; prt_indented(s) }
    function prt_end(s) { prt_indented(s); Indent -= 2 }
    function prt_indented(s) { printf("%*s%s\n", Indent, "", s) }

' "${@:--}"
$ ./tst.sh test.unl
<html>
  <body>
    <table border=1 cellspacing=0 cellpadding=3>
      <tr>
        <td>SID<<th>SID</td>th>
        <td>PID<<th>PID</td>th>
        <td>Username<<th>Username</td>th>
        <td>Database<<th>Database</td>th>
        <td>Hostname<<th>Hostname</td>th>
        <td>Program<<th>Program</td>th>
        <td>Connected<<th>Connected</td>th>
        <td>Idle<th>Idle Time</td>th>
        <td>Query<th>Query Time</td>th>
        <td>EST<th>EST COST</td>th>
        <td>SEQ<th>SEQ SCAN</td>th>
        <td>Query<<th>Query</td>th>
      </tr>
      <tr>
        <td>15422216</td>
        <td>-1</td>
        <td>dwhvo</td>
        <td>test</td>
        <td>pd244zax.test.corp</td>
        <td>N/A</td>
        <td>10:56:53</td>
        <td>-0:00:30</td>
        <td>10:57:22</td>
        <td>1045127</td>
        <td>1</td>
        <td>SELECT sba_sub_aux.sba_subscriber_id, sba_sub_aux.sba_id_number, sba_sub_aux.sba_matchcode, sba_sub_aux.sba_marketing, sba_su</td>
      </tr>
    </table>
  </body>
</html>
$ cat tst.sh
#!/usr/bin/env bash

awk '

    BEGIN {
        Indent = -2
        prt_html()
        exit
    }

    function prt_html() {
        prt_beg("<html>")
        prt_body()
        prt_end("</html>")
    }

    function prt_body() {
        prt_beg("<body>")
        prt_table()
        prt_end("</body>")
    }

    function prt_table() {
        prt_beg("<table border=1 cellspacing=0 cellpadding=3>")
        numHdrCells = prt_row(0,"SID|PID|Username|Database|Hostname|Program|Connected|Idle Time|Query Time|EST COST|SEQ SCAN|Query")
        prt_rows(numHdrCells)
        prt_end("</table>")
    }

    function prt_rows(numHdrCells) {
        while ( getline > 0 ) {
            prt_row(numHdrCells,$0)
        }
    }

    function prt_row(numHdrCells,s,    n) {
        prt_beg("<tr>")
        n = prt_cells(numHdrCells,s)
        prt_end("</tr>")
        return n
    }

    function prt_cells(numHdrCells,s,    n,f,i) {
        n = split(s, f, /[|]/)
        if ( numHdrCells != 0 ) {
            # Force the number of cells printed for each data row
            # to match the number of cells printed for the header.
            n = numHdrCells
        }
        for (i = 1; i <= n; i++) {
            prt_cell(f[i])
        }
        return n
    }

    function prt_cell(s) {
        Indent += 2
        # Strip any leading/trailing white space around this cell
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
        prt_indented("<td>" escape_html(s) "</td>")
        Indent -= 2
    }

    function escape_html(s) {
        gsub(/&/, "\\&amp;", s)
        gsub(/</, "\\&lt;", s)
        gsub(/>/, "\\&gt;", s)
        return s
    }

    function prt_beg(s) { Indent += 2; prt_indented(s) }
    function prt_end(s) { prt_indented(s); Indent -= 2 }
    function prt_indented(s) { printf("%*s%s\n", Indent, "", s) }

' "${@:--}"
$ ./tst.sh test.unl
<html>
  <body>
    <table border=1 cellspacing=0 cellpadding=3>
      <tr>
        <td>SID</td>
        <td>PID</td>
        <td>Username</td>
        <td>Database</td>
        <td>Hostname</td>
        <td>Program</td>
        <td>Connected</td>
        <td>Idle Time</td>
        <td>Query Time</td>
        <td>EST COST</td>
        <td>SEQ SCAN</td>
        <td>Query</td>
      </tr>
      <tr>
        <td>15422216</td>
        <td>-1</td>
        <td>dwhvo</td>
        <td>test</td>
        <td>pd244zax.test.corp</td>
        <td>N/A</td>
        <td>10:56:53</td>
        <td>-0:00:30</td>
        <td>10:57:22</td>
        <td>1045127</td>
        <td>1</td>
        <td>SELECT sba_sub_aux.sba_subscriber_id, sba_sub_aux.sba_id_number, sba_sub_aux.sba_matchcode, sba_sub_aux.sba_marketing, sba_su</td>
      </tr>
    </table>
  </body>
</html>
$ cat tst.sh
#!/usr/bin/env bash

awk '

    BEGIN {
        Indent = -2
        prt_html()
        exit
    }

    function prt_html() {
        prt_beg("<html>")
        prt_body()
        prt_end("</html>")
    }

    function prt_body() {
        prt_beg("<body>")
        prt_table()
        prt_end("</body>")
    }

    function prt_table() {
        prt_beg("<table border=1 cellspacing=0 cellpadding=3>")
        numHdrCells = prt_row(0,"SID|PID|Username|Database|Hostname|Program|Connected|Idle Time|Query Time|EST COST|SEQ SCAN|Query")
        prt_rows(numHdrCells)
        prt_end("</table>")
    }

    function prt_rows(numHdrCells,      line) {
        while ( (getline line) > 0 ) {
            prt_row(numHdrCells,line)
        }
    }

    function prt_row(numHdrCells,s,     numCells) {
        prt_beg("<tr>")
        numCells = prt_cells(numHdrCells,s)
        prt_end("</tr>")
        return numCells
    }

    function prt_cells(numHdrCells,s,    cellType,numCells,f,i) {
        numCells = split(s, f, /[|]/)
        if ( numHdrCells == 0 ) {
            # We are printing the header row
            cellType = "th"
        }
        else {
            # We are printing a data row
            cellType = "td"
            # Force the number of cells printed for each data row
            # to match the number of cells printed for the header.
            numCells = numHdrCells
        }
        for (i = 1; i <= numCells; i++) {
            prt_cell(cellType,f[i])
        }
        return numCells
    }

    function prt_cell(cellType,s) {
        Indent += 2
        # Strip any leading/trailing white space around this cell
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
        prt_indented("<" cellType ">" escape_html(s) "</" cellType ">")
        Indent -= 2
    }

    function escape_html(s) {
        gsub(/&/, "\\&amp;", s)
        gsub(/</, "\\&lt;", s)
        gsub(/>/, "\\&gt;", s)
        return s
    }

    function prt_beg(s) { Indent += 2; prt_indented(s) }
    function prt_end(s) { prt_indented(s); Indent -= 2 }
    function prt_indented(s) { printf("%*s%s\n", Indent, "", s) }

' "${@:--}"
$ ./tst.sh test.unl
<html>
  <body>
    <table border=1 cellspacing=0 cellpadding=3>
      <tr>
        <th>SID</th>
        <th>PID</th>
        <th>Username</th>
        <th>Database</th>
        <th>Hostname</th>
        <th>Program</th>
        <th>Connected</th>
        <th>Idle Time</th>
        <th>Query Time</th>
        <th>EST COST</th>
        <th>SEQ SCAN</th>
        <th>Query</th>
      </tr>
      <tr>
        <td>15422216</td>
        <td>-1</td>
        <td>dwhvo</td>
        <td>test</td>
        <td>pd244zax.test.corp</td>
        <td>N/A</td>
        <td>10:56:53</td>
        <td>-0:00:30</td>
        <td>10:57:22</td>
        <td>1045127</td>
        <td>1</td>
        <td>SELECT sba_sub_aux.sba_subscriber_id, sba_sub_aux.sba_id_number, sba_sub_aux.sba_matchcode, sba_sub_aux.sba_marketing, sba_su</td>
      </tr>
    </table>
  </body>
</html>
added 482 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

@Fravadona's answer is good, here's just an alternative approach to consider that's IMO not any better aside from a couple of cosmetic differences, it just isolates each specific part of the output a bit further by providing a separate function for each level of HTML (html/body/table/rows/row/cells/cell) which might make it a bit less error prone to modify that one part of it in future:

$ cat tst.sh
#!/usr/bin/env bash

awk '

    BEGIN {
        Indent = -2
        prt_html()
        exit
    }

    function prt_html() {
        prt_beg("<html>")
        prt_body()
        prt_end("</html>")
    }

    function prt_body() {
        prt_beg("<body>")
        prt_table()
        prt_end("</body>")
    }

    function prt_table() {
        prt_beg("<table border=1 cellspacing=0 cellpadding=3>")
        numHdrCells = prt_row(0,"SID|PID|Username|Database|Hostname|Program|Connected|Idle Time|Query Time|EST COST|SEQ SCAN|Query")
        prt_rows(numHdrCells)
        prt_end("</table>")
    }

    function prt_rows(numHdrCells) {
        while ( getline > 0 ) {
            prt_row(numHdrCells,$0)
        }
    }

    function prt_row(numHdrCells,s,    n,f,i) {
        prt_beg("<tr>")
        n = prt_cells(numHdrCells,s)
        prt_end("</tr>")
        return n
    }

    function prt_cells(numHdrCells,s,    n,f,i) {
        n = split(s, f, /[[:space:]]*[|][[:space:]]*[|]/)
        if ( numHdrCells != 0 ) {
            # Force the number of cells printed for each data row
            # to match the number of cells printed for the header.
            n = numHdrCells
        }
        for (i = 1; i <= n; i++) {
            prt_cell(f[i])
        }
        return n
    }

    function prt_cell(s) {
        Indent += 2
        # Strip any leading/trailing white space around this cell
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
        prt_indented("<td>" escape_html(s) "</td>")
        Indent -= 2
    }

    function escape_html(s) {
        gsub(/&/, "\\&amp;", s)
        gsub(/</, "\\&lt;", s)
        gsub(/>/, "\\&gt;", s)
        return s
    }

    function prt_beg(s) { Indent += 2; prt_indented(s) }
    function prt_end(s) { prt_indented(s); Indent -= 2 }
    function prt_indented(s) { printf("%*s%s\n", Indent, "", s) }

' "${@:--}"
$ ./tst.sh test.unl
<html>
  <body>
    <table border=1 cellspacing=0 cellpadding=3>
      <tr>
        <td>SID</td>
        <td>PID</td>
        <td>Username</td>
        <td>Database</td>
        <td>Hostname</td>
        <td>Program</td>
        <td>Connected</td>
        <td>Idle Time</td>
        <td>Query Time</td>
        <td>EST COST</td>
        <td>SEQ SCAN</td>
        <td>Query</td>
      </tr>
      <tr>
        <td>15422216</td>
        <td>-1</td>
        <td>dwhvo</td>
        <td>test</td>
        <td>pd244zax.test.corp</td>
        <td>N/A</td>
        <td>10:56:53</td>
        <td>-0:00:30</td>
        <td>10:57:22</td>
        <td>1045127</td>
        <td>1</td>
        <td>SELECT sba_sub_aux.sba_subscriber_id, sba_sub_aux.sba_id_number, sba_sub_aux.sba_matchcode, sba_sub_aux.sba_marketing, sba_su</td>
        <td></td>
      </tr>
    </table>
  </body>
</html>

With split(s, f, /[[:space:]]*[|][[:space:]]*/ I'm removing any white space from the start/end of each input field. If that's undesirable then change [[:space:]]*[|][[:space:]]* to |.

@Fravadona's answer is good, here's just an alternative approach to consider that's IMO not any better, it just isolates each specific part of the output a bit further by providing a separate function for each level of HTML (html/body/table/rows/row/cells/cell) which might make it a bit less error prone to modify that one part of it in future:

$ cat tst.sh
#!/usr/bin/env bash

awk '

    BEGIN {
        Indent = -2
        prt_html()
        exit
    }

    function prt_html() {
        prt_beg("<html>")
        prt_body()
        prt_end("</html>")
    }

    function prt_body() {
        prt_beg("<body>")
        prt_table()
        prt_end("</body>")
    }

    function prt_table() {
        prt_beg("<table border=1 cellspacing=0 cellpadding=3>")
        prt_row("SID|PID|Username|Database|Hostname|Program|Connected|Idle Time|Query Time|EST COST|SEQ SCAN|Query")
        prt_rows()
        prt_end("</table>")
    }

    function prt_rows() {
        while ( getline > 0 ) {
            prt_row($0)
        }
    }

    function prt_row(s,    n,f,i) {
        prt_beg("<tr>")
        prt_cells(s)
        prt_end("</tr>")
    }

    function prt_cells(s,    n,f,i) {
        n = split(s, f, /[[:space:]]*[|][[:space:]]*/)
        for (i = 1; i <= n; i++) {
            prt_cell(f[i])
        }
    }

    function prt_cell(s) {
        Indent += 2
        prt_indented("<td>" escape_html(s) "</td>")
        Indent -= 2
    }

    function escape_html(s) {
        gsub(/&/, "\\&amp;", s)
        gsub(/</, "\\&lt;", s)
        gsub(/>/, "\\&gt;", s)
        return s
    }

    function prt_beg(s) { Indent += 2; prt_indented(s) }
    function prt_end(s) { prt_indented(s); Indent -= 2 }
    function prt_indented(s) { printf("%*s%s\n", Indent, "", s) }

' "${@:--}"
$ ./tst.sh test.unl
<html>
  <body>
    <table border=1 cellspacing=0 cellpadding=3>
      <tr>
        <td>SID</td>
        <td>PID</td>
        <td>Username</td>
        <td>Database</td>
        <td>Hostname</td>
        <td>Program</td>
        <td>Connected</td>
        <td>Idle Time</td>
        <td>Query Time</td>
        <td>EST COST</td>
        <td>SEQ SCAN</td>
        <td>Query</td>
      </tr>
      <tr>
        <td>15422216</td>
        <td>-1</td>
        <td>dwhvo</td>
        <td>test</td>
        <td>pd244zax.test.corp</td>
        <td>N/A</td>
        <td>10:56:53</td>
        <td>-0:00:30</td>
        <td>10:57:22</td>
        <td>1045127</td>
        <td>1</td>
        <td>SELECT sba_sub_aux.sba_subscriber_id, sba_sub_aux.sba_id_number, sba_sub_aux.sba_matchcode, sba_sub_aux.sba_marketing, sba_su</td>
        <td></td>
      </tr>
    </table>
  </body>
</html>

With split(s, f, /[[:space:]]*[|][[:space:]]*/ I'm removing any white space from the start/end of each input field. If that's undesirable then change [[:space:]]*[|][[:space:]]* to |.

@Fravadona's answer is good, here's just an alternative approach to consider that's IMO not any better aside from a couple of cosmetic differences, it just isolates each specific part of the output a bit further by providing a separate function for each level of HTML (html/body/table/rows/row/cells/cell) which might make it a bit less error prone to modify that one part of it in future:

$ cat tst.sh
#!/usr/bin/env bash

awk '

    BEGIN {
        Indent = -2
        prt_html()
        exit
    }

    function prt_html() {
        prt_beg("<html>")
        prt_body()
        prt_end("</html>")
    }

    function prt_body() {
        prt_beg("<body>")
        prt_table()
        prt_end("</body>")
    }

    function prt_table() {
        prt_beg("<table border=1 cellspacing=0 cellpadding=3>")
        numHdrCells = prt_row(0,"SID|PID|Username|Database|Hostname|Program|Connected|Idle Time|Query Time|EST COST|SEQ SCAN|Query")
        prt_rows(numHdrCells)
        prt_end("</table>")
    }

    function prt_rows(numHdrCells) {
        while ( getline > 0 ) {
            prt_row(numHdrCells,$0)
        }
    }

    function prt_row(numHdrCells,s,    n) {
        prt_beg("<tr>")
        n = prt_cells(numHdrCells,s)
        prt_end("</tr>")
        return n
    }

    function prt_cells(numHdrCells,s,    n,f,i) {
        n = split(s, f, /[|]/)
        if ( numHdrCells != 0 ) {
            # Force the number of cells printed for each data row
            # to match the number of cells printed for the header.
            n = numHdrCells
        }
        for (i = 1; i <= n; i++) {
            prt_cell(f[i])
        }
        return n
    }

    function prt_cell(s) {
        Indent += 2
        # Strip any leading/trailing white space around this cell
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
        prt_indented("<td>" escape_html(s) "</td>")
        Indent -= 2
    }

    function escape_html(s) {
        gsub(/&/, "\\&amp;", s)
        gsub(/</, "\\&lt;", s)
        gsub(/>/, "\\&gt;", s)
        return s
    }

    function prt_beg(s) { Indent += 2; prt_indented(s) }
    function prt_end(s) { prt_indented(s); Indent -= 2 }
    function prt_indented(s) { printf("%*s%s\n", Indent, "", s) }

' "${@:--}"
$ ./tst.sh test.unl
<html>
  <body>
    <table border=1 cellspacing=0 cellpadding=3>
      <tr>
        <td>SID</td>
        <td>PID</td>
        <td>Username</td>
        <td>Database</td>
        <td>Hostname</td>
        <td>Program</td>
        <td>Connected</td>
        <td>Idle Time</td>
        <td>Query Time</td>
        <td>EST COST</td>
        <td>SEQ SCAN</td>
        <td>Query</td>
      </tr>
      <tr>
        <td>15422216</td>
        <td>-1</td>
        <td>dwhvo</td>
        <td>test</td>
        <td>pd244zax.test.corp</td>
        <td>N/A</td>
        <td>10:56:53</td>
        <td>-0:00:30</td>
        <td>10:57:22</td>
        <td>1045127</td>
        <td>1</td>
        <td>SELECT sba_sub_aux.sba_subscriber_id, sba_sub_aux.sba_id_number, sba_sub_aux.sba_matchcode, sba_sub_aux.sba_marketing, sba_su</td>
      </tr>
    </table>
  </body>
</html>
added 455 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
Loading
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
Loading