简体   繁体   English

sed变量替换无效

[英]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 这就是$ photostr

 <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. 对于输入,$ 1是一个包含html和一些{{metatag}}的文件,需要适当的替换才能制作一个正常的网页。 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: 在使用变量之前,将变量值中的换行符编码为\\n

photostr=$(sed ':a;N;$!ba;s/\n/\\n/g' <<< "$photostr")
sed "s|{{/each}}| -->\n $photostr |" $1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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