0

I have a small project that takes a HTML template file in it like so;

<p>Hi, I'm some text for this project!</p>

and I want it to be read by my bash script but Sed is escaping out at the closing tag which is less than optimmal;

At the moment it looks like this ($postContents being the above HTML text);

## Replace <!-- TITLE --> in Head > Title and in Headder > H1 with RealName
sed -i "s/{{TITLE}}/$realname/g" $webdir/blog/$basefile
## Replace <!-- DATE --> With webdate
sed -i "s/{{DATE}}/$webdate/g" $webdir/blog/$basefile
## Replace <!-- CONTENTS --> With postContents
sed -i "s/{{CONTENTS}}/"$postContents"/g" $webdir/blog/$basefile

Having manually checked, I'm absolutely sure it's catching on the closing tag. What do I need to do to get this working?

Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
  • 4
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Jun 18 '20 at 18:24
  • Show us sample input & expected output, we will give you nice pointers – Gilles Quenot Jun 18 '20 at 18:30
  • Is it a template file ? If yes, which tool is supposed to edit it ? – Gilles Quenot Jun 18 '20 at 18:32
  • 1
    OK, sorry, yeah. I should have mentioned this but bashing (sorry) my head on this for a few hours kinda made me short a bit. The words in the handlebars are targets in a template file that is copied from the template folder ($webdir/template/postTemp) to the blog folder ($webdir/template/slug) and given verious atributes between date, readable name, all that fun stuff. – Craig Mcleod Jun 18 '20 at 18:55
  • 1
    I dunno if someone else delted it but I got it working by swaping the / dilimiator with pipes. – Craig Mcleod Jun 18 '20 at 19:05
  • @CraigMcleod You may be referring to what I wrote, which was to the effect that a different delimiter could be used. It was essentially an extended comment rather than an answer (I described it as being of possible use as a starting point) because of the clearly expressed limitation that if whatever delimiter you choose is present in the replacement string then you are back to exactly the same problem again, but despite this explicit caveat it attracted downvoting, so I withdrew it. Do you need to see it again? – alani Jun 18 '20 at 19:09
  • This is a possible duplicate of: [Escape a string for a sed replace pattern](https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern) (which you should read for other possible solutions). – Gordon Davisson Jun 18 '20 at 19:11
  • Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. Please see [the relevant help-center article](https://stackoverflow.com/help/someone-answers) – Gilles Quenot Jun 24 '20 at 02:28

1 Answers1

0

Better use a real template engine.

I think of perl Template::Toolkit module or python Jinja.

Using the tpage utility, installed with the perl module Template::Toolkit (package libtemplate-perl on Debian):

cat template.tpl
<!DOCTYPE html>
<html lang="fr">
<head>
</style>
<title>[% title %]</title>
</head>
<body>
<a href="[% href %]" title="[% htitle %]" ></a>
<br/>
</body>
</html>

The code

$ tpage --define title=bar --define href=https://x.j/qs --define htitle=bar template.tpl 

Output

<!DOCTYPE html>
<html lang="fr">
<head>
</style>
<title>bar</title>
</head>
<body>
<a href="https://x.j/qs" title="bar" ></a>
<br/>
</body>
</html>
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206