2

I have been working on a script that collects data about servers and formats them into a nice html email report. I have been using sed to replace values in a html template so far and that has been mostly okay, but I have come across a situation where I need to insert a multiple line block of html based on variables an arbitrary number of times. I'm not sure quite how to do this, as sed doesn't seem to play too nice with multiple lines? Maybe there's a way with perl or awk that I hadn't considered.

Here's an example where I'd need to insert 0-n table blocks into an existing template based on however many files were there

for f in $files

  <tr class="details">
        <td> f.$DATE </td>
        <td> f.$APPLIANCE </td>
        <td> f.$HOST </td>
        <td> f.$MDL </td>
  </tr>

etc.

Thanks!

  • 1
    If you are trying to build HTML on the fly, this is a problem domain that has been done many times. Generally known as templating systems, they allow relatively easy programmatic creation of arbitrarily complex html. Perl being the grand-dad of web languages has this covered, but so do more modern choices like php, python and node. (And I am sure dozens of others). – Stephen Rauch Apr 13 '17 at 3:11
1

Use a here-document:

for f in $files; do
    cat <<END_HTML
  <tr class="details">
        <td> f.$DATE </td>
        <td> f.$APPLIANCE </td>
        <td> f.$HOST </td>
        <td> f.$MDL </td>
  </tr>
END_HTML
done

This will output the block as many times as there are words in $files, with the named variables expanded to their respective values.

0

You can try the "snippets",many text editor or IDE support this feature. You can customize some template for coding block and name it, then you can type the name and tab to expand it to the coding block. Further, you can tab to edit the placeholders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.