开发者

bash templating

i have a template, with a var LINK and a data file, 开发者_如何转开发links.txt, with one url per line

how in bash i can substitute LINK with the content of links.txt?

if i do

#!/bin/bash
LINKS=$(cat links.txt)
sed "s/LINKS/$LINK/g" template.xml

two problem:

  1. $LINKS has the content of links.txt without newline

  2. sed: 1: "s/LINKS/http://test ...": bad flag in substitute command: '/'

sed is not escaping the // in the links.txt file

thanks


Use some better language instead. I'd write a solution for bash + awk... but that's simply too much effort to go into. (See http://www.gnu.org/manual/gawk/gawk.html#Getline_002fVariable_002fFile if you really want to do that)

Just use any language where you don't have to mix control and content text. For example in python:

#!/usr/bin/env python
links = open('links.txt').read()
template = open('template.xml').read()
print template.replace('LINKS', links)

Watch out if you're trying to force sed solution with some other separator - you'll get into the same problems unless you find something disallowed in urls (but are you verifying that?) If you don't, you already have another problem - links can contain < and > and break your xml.


You can do this using ed:

ed template.xml <<EOF
/LINKS/d
.r links.txt
w output.txt
EOF
  • The first command will go to the line containing LINKS and delete it.
  • The second line will insert the contents of links.txt on the current line.
  • The third command will write the file to output.txt (if you omit output.txt the edits will be saved to template.xml).


Try running sed twice. On the first run, replace / with \/. The second run will be the same as what you currently have.


The character following the 's' in the sed command ends up the separator, so you'll want to use a character that is not present in the value of $LINK. For example, you could try a comma:

sed "s,LINKS,${LINK}\n,g" template.xml

Note that I also added a \n to add an additional newline.

Another option is to escape the forward slashes in $LINK, possibly using sed. If you don't have guarantees about the characters in $LINK, this may be safer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜