branquito
branquito

Reputation: 4064

Render sqlite output into template file, using bash script

I have template-like file, with some simple HTML in it.

<html>
    <head>
        <meta charset="utf-8">
        <title>stackoverflow.com</title>
        <link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" title="no title" charset="utf-8">

    </head>
    <body id="container">
    <table border="0" cellspacing="5" cellpadding="5">
        <tr>
            <th>Field 1 Label</th>
            <th>Field 2 Label</th>
            <th>Field 3 Label</th>
        </tr>

        <!-- ...dynamically generated content from other file should go here... -->
        <!-- ...instead of this template tag... -->
        ${sqlite_html_output} <!-- this is a placeholder I want to use -->

    </table>


    </body>
</html>

The file I want to render into this template, will be the output of sqlite query contained in the following function:

function generate_report() {

    sqlite3 -batch database.db <<- "end_of_message"
        .mode html
        .output o.html # <-- this is the name of generated output file
        select field1,field2,field3 from tbl;
    end_of_message

    #..here I want to use `sed` to embed content of o.html file, into my template
}

So in my generate_report function, I want to use sed to render that o.html file, into my template, in place of that ${sqlite_html_output} template-tag placeholder.

Would that be possible?

Upvotes: 0

Views: 543

Answers (1)

Qeole
Qeole

Reputation: 9184

Try this:

sed '/\${sqlite_html_output}/{r o.html
    d;}' template_file

It should search for a line matching ${sqlite_html_output} and, when matching it, insert text read from o.html then delete content of line (i.e. ${sqlite_html_output} <!-- this is a placeholder I want to use --> in your example).

Line break is mandatory.

Upvotes: 1

Related Questions