Urco
Urco

Reputation: 365

Substitute variable in sed inside Makefile

I've been throught all possible answers in SO but still I cannot make my script work.

I have a query script in SPARQL with a line that needs to substitute the mark %contributor% by a variable between "<>"

?link dcterms:contributor %contributor%

Into

 ?link dcterms:contributor <http://newurl>

If I execute the code in the shell, the substitution is done properly and I see my query with the %contributor% tag successfully substituted. But when executed inside a Makefile, even with the double $$ dollar to allow the expansion of variables I don't manage to get it done.

for l in `cat sref.csv`; do \
    QUERY=$$(cat queries/table_knowledge.rq | sed "s@%contributor%@<$$l>@g") ; \
    echo $$QUERY ; \
done

Also like this it does not work:

QUERY=$$(cat queries/table_knowledge.rq | sed 's/%contributor%/<$(l)>/g') ; \

or

QUERY=$$(cat queries/table_knowledge.rq | sed 's/%contributor%/<${l}>/g') ; \

Upvotes: 1

Views: 1265

Answers (1)

Ronak Patel
Ronak Patel

Reputation: 3849

Use double quotes so that sed would expand variables.
Use a separator different than / since the replacement contains /

QUERY=$(cat queries/table_knowledge.rq | sed "s|%contributor%|<${l}>|g") ;

Test:

$ test="http://test.com"
$ echo "?link dcterms:contributor %contributor%" > source.file
$ x=$(cat source.file|sed "s|%contributor%|<${test}>|g")
$ echo $x
?link dcterms:contributor <http://test.com>

Upvotes: 2

Related Questions