简体   繁体   中英

sed variable substitution not working

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM