tags:

views:

47

answers:

4

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

A: 

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

bta
This is unnecessary. You do not have to use the / character to in substitutions. You can use any character you want, so pick one that wont conflict with a URL. e.g. s|a|b| or s$a$b$ etc. (Note: I don't know if | or $ are invalid URL chars - I'm just using them as examples of characters different to /)
camh
@camh- Good point. The `$` character is a valid URL character, so I would go with `|` or `^`.
bta
A: 

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.

Kaleb Pederson
+1  A: 

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.

viraptor
A: 

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).
Bart Sas