1

This problems been driving me up the wall for a bit now, I've searched a ton and it seems like nobody elses solutions work for me...

sed "s|{{/each}}| -->\n $photostr |" $1

So I'm trying to end a comment, and slap in my photo string. Here's what $photostr is

 <a data-gallery="gallery" href="The_Great_Wave.jpg"  title=" The Great Wave off Kanagawa"> <img src="bar" alt=" The Great Wave off Kanagawa"/></a>
 <a data-gallery="gallery" href="Mt_Fuji.jpg"  title=" Mount Fuji (the highest mountain in Japan)"> <img src="bar" alt=" Mount Fuji (the highest mountain in Japan)"/></a>
 <a data-gallery="gallery" href="Beach.jpg"  title=" Waves Crashing on the Beach"> <img src="bar" alt=" Waves Crashing on the Beach"/></a>
 <a data-gallery="gallery" href="Elephant.jpg"  title=" An Elephant in the Serengeti"> <img src="bar" alt=" An Elephant in the Serengeti"/></a>
 <a data-gallery="gallery" href="Milky_Way.jpg"  title=" The Milky Way Galaxy (contains our Solar System)"> <img src="bar" alt=" The Milky Way Galaxy (contains our Solar System)"/></a>
 <a data-gallery="gallery" href="Poppies.jpg"  title=" Poppies in Bloom"> <img src="bar" alt=" Poppies in Bloom"/></a>

So it's chok full of meta characters, so I'm using pipes as delimeters, but I get this error...

sed: -e expression #1, char 70: unterminated `s' command 

For input, $1 is a file that has html and some {{metatag}} in it that need appropriate substitutions to make a working webpage. The bit I'm concerned with,

</a> {{/each}} </div>

should be turned into...

</a> --> <a data-gallery="gallery" href="The_Great_Wave.jpg" title=" The Great Wave off Kanagawa"> <img src="bar" alt=" The Great Wave off Kanagawa"/></a> <a data-gallery="gallery" href="Mt_Fuji.jpg" title=" Mount Fuji (the highest mountain in Japan)"> <img src="bar" alt=" Mount Fuji (the highest mountain in Japan)"/></a> <a data-gallery="gallery" href="Beach.jpg" title=" Waves Crashing on the Beach"> <img src="bar" alt=" Waves Crashing on the Beach"/></a> <a data-gallery="gallery" href="Elephant.jpg" title=" An Elephant in the Serengeti"> <img src="bar" alt=" An Elephant in the Serengeti"/></a> <a data-gallery="gallery" href="Milky_Way.jpg" title=" The Milky Way Galaxy (contains our Solar System)"> <img src="bar" alt=" The Milky Way Galaxy (contains our Solar System)"/></a> <a data-gallery="gallery" href="Poppies.jpg" title=" Poppies in Bloom"> <img src="bar" alt=" Poppies in Bloom"/></a>

SetSlapShot
  • 1,298
  • 1
  • 21
  • 48
  • 1
    What is the input and desired output? – John Zwinck Mar 17 '13 at 14:47
  • edited with expected io – SetSlapShot Mar 17 '13 at 14:52
  • 1
    Why use sed and not an actual x/html processing/template language xpath,xquery,xslt,php,... ? – BeniBela Mar 17 '13 at 14:59
  • Because I was told that my technology stack was limited to bash. It seems like it would be a simple variable substitution with sed I'm just missing something basic. – SetSlapShot Mar 17 '13 at 15:01
  • Don't use regex for HTML, read http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Gilles Quénot Mar 17 '13 at 15:02
  • 1
    Why vote to close this? It's a question about trying to solve a problem in sed. Even if it's not completely practical, I wish someone with more sed and bash experience than I could show either a complete or partial solution using sed. – octopusgrabbus Mar 17 '13 at 15:20

1 Answers1

2

Encode the line breaks in the value of your variable as \n before you use it:

photostr=$(sed ':a;N;$!ba;s/\n/\\n/g' <<< "$photostr")
sed "s|{{/each}}| -->\n $photostr |" $1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328