sed变量替换不起作用

时间:2013-03-17 14:41:42

标签: bash unix sed

这个问题一直让我陷入困境,我已经搜索了一下,似乎没有人能够解决这个问题......

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

所以我试图结束评论,并拍打我的照片字符串。这是$ 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>

所以它充满了元字符,所以我使用管道作为分隔符,但是我得到了这个错误......

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

对于输入,$ 1是一个文件,其中包含html和一些{{metatag}},需要适当的替换才能生成有效的网页。我关心的一点,

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

应该变成......

</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>

1 个答案:

答案 0 :(得分:2)

在使用之前,将换行符的换行符编码为\n

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